155
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwt.custom.CLabel;
|
|
14
|
|
15
|
|
16 import dwt.DWT;
|
|
17 import dwt.DWTException;
|
|
18 import dwt.accessibility.ACC;
|
|
19 import dwt.accessibility.Accessible;
|
|
20 import dwt.accessibility.AccessibleAdapter;
|
|
21 import dwt.accessibility.AccessibleControlAdapter;
|
|
22 import dwt.accessibility.AccessibleControlEvent;
|
|
23 import dwt.accessibility.AccessibleEvent;
|
|
24 import dwt.events.DisposeEvent;
|
|
25 import dwt.events.DisposeListener;
|
|
26 import dwt.events.PaintEvent;
|
|
27 import dwt.events.PaintListener;
|
|
28 import dwt.events.TraverseEvent;
|
|
29 import dwt.events.TraverseListener;
|
|
30 import dwt.graphics.Color;
|
|
31 import dwt.graphics.Font;
|
|
32 import dwt.graphics.GC;
|
|
33 import dwt.graphics.Image;
|
|
34 import dwt.graphics.Point;
|
|
35 import dwt.graphics.Rectangle;
|
|
36 import dwt.widgets.Canvas;
|
|
37 import dwt.widgets.Composite;
|
|
38 import dwt.widgets.Control;
|
|
39 import dwt.widgets.Display;
|
|
40
|
|
41 static import tango.text.Unicode;
|
|
42 static import tango.text.convert.Utf;
|
|
43 import dwt.dwthelper.utils;
|
|
44
|
|
45 /**
|
|
46 * A Label which supports aligned text and/or an image and different border styles.
|
|
47 * <p>
|
|
48 * If there is not enough space a CLabel uses the following strategy to fit the
|
|
49 * information into the available space:
|
|
50 * <pre>
|
|
51 * ignores the indent in left align mode
|
|
52 * ignores the image and the gap
|
|
53 * shortens the text by replacing the center portion of the label with an ellipsis
|
|
54 * shortens the text by removing the center portion of the label
|
|
55 * </pre>
|
|
56 * <p>
|
|
57 * <dl>
|
|
58 * <dt><b>Styles:</b>
|
|
59 * <dd>LEFT, RIGHT, CENTER, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd>
|
|
60 * <dt><b>Events:</b>
|
|
61 * <dd></dd>
|
|
62 * </dl>
|
|
63 *
|
|
64 * </p><p>
|
|
65 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
|
|
66 * </p>
|
|
67 */
|
|
68 public class CLabel : Canvas {
|
|
69
|
|
70 alias Canvas.computeSize computeSize;
|
|
71
|
|
72 /** Gap between icon and text */
|
|
73 private static const int GAP = 5;
|
|
74 /** Left and right margins */
|
|
75 private static const int INDENT = 3;
|
|
76 /** a string inserted in the middle of text that has been shortened */
|
|
77 private static const char[] ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026"
|
|
78 /** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/
|
|
79 private int align_ = DWT.LEFT;
|
|
80 private int hIndent = INDENT;
|
|
81 private int vIndent = INDENT;
|
|
82 /** the current text */
|
|
83 private char[] text;
|
|
84 /** the current icon */
|
|
85 private Image image;
|
|
86 // The tooltip is used for two purposes - the application can set
|
|
87 // a tooltip or the tooltip can be used to display the full text when the
|
|
88 // the text has been truncated due to the label being too short.
|
|
89 // The appToolTip stores the tooltip set by the application. Control.tooltiptext
|
|
90 // contains whatever tooltip is currently being displayed.
|
|
91 private char[] appToolTipText;
|
|
92
|
|
93 private Image backgroundImage;
|
|
94 private Color[] gradientColors;
|
|
95 private int[] gradientPercents;
|
|
96 private bool gradientVertical;
|
|
97 private Color background;
|
|
98
|
|
99 private static int DRAW_FLAGS = DWT.DRAW_MNEMONIC | DWT.DRAW_TAB | DWT.DRAW_TRANSPARENT | DWT.DRAW_DELIMITER;
|
|
100
|
|
101 /**
|
|
102 * Constructs a new instance of this class given its parent
|
|
103 * and a style value describing its behavior and appearance.
|
|
104 * <p>
|
|
105 * The style value is either one of the style constants defined in
|
|
106 * class <code>DWT</code> which is applicable to instances of this
|
|
107 * class, or must be built by <em>bitwise OR</em>'ing together
|
|
108 * (that is, using the <code>int</code> "|" operator) two or more
|
|
109 * of those <code>DWT</code> style constants. The class description
|
|
110 * lists the style constants that are applicable to the class.
|
|
111 * Style bits are also inherited from superclasses.
|
|
112 * </p>
|
|
113 *
|
|
114 * @param parent a widget which will be the parent of the new instance (cannot be null)
|
|
115 * @param style the style of widget to construct
|
|
116 *
|
|
117 * @exception IllegalArgumentException <ul>
|
|
118 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
119 * </ul>
|
|
120 * @exception DWTException <ul>
|
|
121 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
122 * </ul>
|
|
123 *
|
|
124 * @see DWT#LEFT
|
|
125 * @see DWT#RIGHT
|
|
126 * @see DWT#CENTER
|
|
127 * @see DWT#SHADOW_IN
|
|
128 * @see DWT#SHADOW_OUT
|
|
129 * @see DWT#SHADOW_NONE
|
|
130 * @see #getStyle()
|
|
131 */
|
|
132 public this(Composite parent, int style) {
|
|
133 super(parent, checkStyle(style));
|
|
134 if ((style & (DWT.CENTER | DWT.RIGHT)) is 0) style |= DWT.LEFT;
|
|
135 if ((style & DWT.CENTER) !is 0) align_ = DWT.CENTER;
|
|
136 if ((style & DWT.RIGHT) !is 0) align_ = DWT.RIGHT;
|
|
137 if ((style & DWT.LEFT) !is 0) align_ = DWT.LEFT;
|
|
138
|
|
139 addPaintListener(new class() PaintListener{
|
|
140 public void paintControl(PaintEvent event) {
|
|
141 onPaint(event);
|
|
142 }
|
|
143 });
|
|
144
|
|
145 addDisposeListener(new class() DisposeListener{
|
|
146 public void widgetDisposed(DisposeEvent event) {
|
|
147 onDispose(event);
|
|
148 }
|
|
149 });
|
|
150
|
|
151 addTraverseListener(new class() TraverseListener {
|
|
152 public void keyTraversed(TraverseEvent event) {
|
|
153 if (event.detail is DWT.TRAVERSE_MNEMONIC) {
|
|
154 onMnemonic(event);
|
|
155 }
|
|
156 }
|
|
157 });
|
|
158
|
|
159 initAccessible();
|
|
160
|
|
161 }
|
|
162 /**
|
|
163 * Check the style bits to ensure that no invalid styles are applied.
|
|
164 */
|
|
165 private static int checkStyle (int style) {
|
|
166 if ((style & DWT.BORDER) !is 0) style |= DWT.SHADOW_IN;
|
|
167 int mask = DWT.SHADOW_IN | DWT.SHADOW_OUT | DWT.SHADOW_NONE | DWT.LEFT_TO_RIGHT | DWT.RIGHT_TO_LEFT;
|
|
168 style = style & mask;
|
|
169 return style |= DWT.NO_FOCUS | DWT.DOUBLE_BUFFERED;
|
|
170 }
|
|
171
|
|
172 //protected void checkSubclass () {
|
|
173 // String name = getClass().getName ();
|
|
174 // String validName = CLabel.class.getName();
|
|
175 // if (!validName.equals(name)) {
|
|
176 // DWT.error (DWT.ERROR_INVALID_SUBCLASS);
|
|
177 // }
|
|
178 //}
|
|
179
|
|
180 public override Point computeSize(int wHint, int hHint, bool changed) {
|
|
181 checkWidget();
|
|
182 Point e = getTotalSize(image, text);
|
|
183 if (wHint is DWT.DEFAULT){
|
|
184 e.x += 2*hIndent;
|
|
185 } else {
|
|
186 e.x = wHint;
|
|
187 }
|
|
188 if (hHint is DWT.DEFAULT) {
|
|
189 e.y += 2*vIndent;
|
|
190 } else {
|
|
191 e.y = hHint;
|
|
192 }
|
|
193 return e;
|
|
194 }
|
|
195 /**
|
|
196 * Draw a rectangle in the given colors.
|
|
197 */
|
|
198 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) {
|
|
199 gc.setForeground(bottomright);
|
|
200 gc.drawLine(x+w, y, x+w, y+h);
|
|
201 gc.drawLine(x, y+h, x+w, y+h);
|
|
202
|
|
203 gc.setForeground(topleft);
|
|
204 gc.drawLine(x, y, x+w-1, y);
|
|
205 gc.drawLine(x, y, x, y+h-1);
|
|
206 }
|
|
207 /*
|
|
208 * Return the lowercase of the first non-'&' character following
|
|
209 * an '&' character in the given string. If there are no '&'
|
|
210 * characters in the given string, return '\0'.
|
|
211 */
|
|
212 dchar _findMnemonic (char[] string) {
|
|
213 if (string is null) return '\0';
|
|
214 int index = 0;
|
|
215 int length = string.length;
|
|
216 do {
|
|
217 while (index < length && string[index] !is '&') index++;
|
|
218 if (++index >= length) return '\0';
|
|
219 if (string[index] !is '&') {
|
|
220 dchar[1] tmp; uint ate;
|
|
221 dchar[] tmp2 = tango.text.convert.Utf.toString32( string[index .. Math.min( index + 4, string.length ) ], tmp, &ate );
|
|
222 assert( tmp2.length == 1 );
|
|
223 return tango.text.Unicode.toLower( tmp2 )[0];
|
|
224 }
|
|
225 index++;
|
|
226 } while (index < length);
|
|
227 return '\0';
|
|
228 }
|
|
229 /**
|
|
230 * Returns the alignment.
|
|
231 * The alignment style (LEFT, CENTER or RIGHT) is returned.
|
|
232 *
|
|
233 * @return DWT.LEFT, DWT.RIGHT or DWT.CENTER
|
|
234 */
|
|
235 public int getAlignment() {
|
|
236 //checkWidget();
|
|
237 return align_;
|
|
238 }
|
|
239 /**
|
|
240 * Return the CLabel's image or <code>null</code>.
|
|
241 *
|
|
242 * @return the image of the label or null
|
|
243 */
|
|
244 public Image getImage() {
|
|
245 //checkWidget();
|
|
246 return image;
|
|
247 }
|
|
248 /**
|
|
249 * Compute the minimum size.
|
|
250 */
|
|
251 private Point getTotalSize(Image image, char[] text) {
|
|
252 Point size = new Point(0, 0);
|
|
253
|
|
254 if (image !is null) {
|
|
255 Rectangle r = image.getBounds();
|
|
256 size.x += r.width;
|
|
257 size.y += r.height;
|
|
258 }
|
|
259
|
|
260 GC gc = new GC(this);
|
|
261 if (text !is null && text.length > 0) {
|
|
262 Point e = gc.textExtent(text, DRAW_FLAGS);
|
|
263 size.x += e.x;
|
|
264 size.y = Math.max(size.y, e.y);
|
|
265 if (image !is null) size.x += GAP;
|
|
266 } else {
|
|
267 size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
|
|
268 }
|
|
269 gc.dispose();
|
|
270
|
|
271 return size;
|
|
272 }
|
|
273 public override int getStyle () {
|
|
274 int style = super.getStyle();
|
|
275 switch (align_) {
|
|
276 case DWT.RIGHT: style |= DWT.RIGHT; break;
|
|
277 case DWT.CENTER: style |= DWT.CENTER; break;
|
|
278 case DWT.LEFT: style |= DWT.LEFT; break;
|
|
279 default:
|
|
280 }
|
|
281 return style;
|
|
282 }
|
|
283
|
|
284 /**
|
|
285 * Return the Label's text.
|
|
286 *
|
|
287 * @return the text of the label or null
|
|
288 */
|
|
289 public char[] getText() {
|
|
290 //checkWidget();
|
|
291 return text;
|
|
292 }
|
|
293 public override char[] getToolTipText () {
|
|
294 checkWidget();
|
|
295 return appToolTipText;
|
|
296 }
|
|
297 private void initAccessible() {
|
|
298 Accessible accessible = getAccessible();
|
|
299 accessible.addAccessibleListener(new class() AccessibleAdapter {
|
|
300 public void getName(AccessibleEvent e) {
|
|
301 e.result = getText();
|
|
302 }
|
|
303
|
|
304 public void getHelp(AccessibleEvent e) {
|
|
305 e.result = getToolTipText();
|
|
306 }
|
|
307
|
|
308 public void getKeyboardShortcut(AccessibleEvent e) {
|
|
309 dchar mnemonic = _findMnemonic(this.outer.text);
|
|
310 if (mnemonic !is '\0') {
|
|
311 dchar[1] d;
|
|
312 d[0] = mnemonic;
|
|
313 e.result = "Alt+" ~ tango.text.convert.Utf.toString(d); //$NON-NLS-1$
|
|
314 }
|
|
315 }
|
|
316 });
|
|
317
|
|
318 accessible.addAccessibleControlListener(new class() AccessibleControlAdapter {
|
|
319 public void getChildAtPoint(AccessibleControlEvent e) {
|
|
320 e.childID = ACC.CHILDID_SELF;
|
|
321 }
|
|
322
|
|
323 public void getLocation(AccessibleControlEvent e) {
|
|
324 Rectangle rect = getDisplay().map(getParent(), null, getBounds());
|
|
325 e.x = rect.x;
|
|
326 e.y = rect.y;
|
|
327 e.width = rect.width;
|
|
328 e.height = rect.height;
|
|
329 }
|
|
330
|
|
331 public void getChildCount(AccessibleControlEvent e) {
|
|
332 e.detail = 0;
|
|
333 }
|
|
334
|
|
335 public void getRole(AccessibleControlEvent e) {
|
|
336 e.detail = ACC.ROLE_LABEL;
|
|
337 }
|
|
338
|
|
339 public void getState(AccessibleControlEvent e) {
|
|
340 e.detail = ACC.STATE_READONLY;
|
|
341 }
|
|
342 });
|
|
343 }
|
|
344 void onDispose(DisposeEvent event) {
|
|
345 gradientColors = null;
|
|
346 gradientPercents = null;
|
|
347 backgroundImage = null;
|
|
348 text = null;
|
|
349 image = null;
|
|
350 appToolTipText = null;
|
|
351 }
|
|
352 void onMnemonic(TraverseEvent event) {
|
|
353 dchar mnemonic = _findMnemonic(text);
|
|
354 if (mnemonic is '\0') return;
|
|
355 dchar[1] d; uint ate;
|
|
356 auto r = tango.text.convert.Utf.toString32( [event.character][], d, &ate );
|
|
357 if (tango.text.Unicode.toLower(r)[0] !is mnemonic) return;
|
|
358 Composite control = this.getParent();
|
|
359 while (control !is null) {
|
|
360 Control [] children = control.getChildren();
|
|
361 int index = 0;
|
|
362 while (index < children.length) {
|
|
363 if (children [index] is this) break;
|
|
364 index++;
|
|
365 }
|
|
366 index++;
|
|
367 if (index < children.length) {
|
|
368 if (children [index].setFocus ()) {
|
|
369 event.doit = true;
|
|
370 event.detail = DWT.TRAVERSE_NONE;
|
|
371 }
|
|
372 }
|
|
373 control = control.getParent();
|
|
374 }
|
|
375 }
|
|
376
|
|
377 void onPaint(PaintEvent event) {
|
|
378 Rectangle rect = getClientArea();
|
|
379 if (rect.width is 0 || rect.height is 0) return;
|
|
380
|
|
381 bool shortenText_ = false;
|
|
382 char[] t = text;
|
|
383 Image img = image;
|
|
384 int availableWidth = Math.max(0, rect.width - 2*hIndent);
|
|
385 Point extent = getTotalSize(img, t);
|
|
386 if (extent.x > availableWidth) {
|
|
387 img = null;
|
|
388 extent = getTotalSize(img, t);
|
|
389 if (extent.x > availableWidth) {
|
|
390 shortenText_ = true;
|
|
391 }
|
|
392 }
|
|
393
|
|
394 GC gc = event.gc;
|
|
395 char[][] lines = text is null ? null : splitString(text);
|
|
396
|
|
397 // shorten the text
|
|
398 if (shortenText_) {
|
|
399 extent.x = 0;
|
|
400 for(int i = 0; i < lines.length; i++) {
|
|
401 Point e = gc.textExtent(lines[i], DRAW_FLAGS);
|
|
402 if (e.x > availableWidth) {
|
|
403 lines[i] = shortenText(gc, lines[i], availableWidth);
|
|
404 extent.x = Math.max(extent.x, getTotalSize(null, lines[i]).x);
|
|
405 } else {
|
|
406 extent.x = Math.max(extent.x, e.x);
|
|
407 }
|
|
408 }
|
|
409 if (appToolTipText is null) {
|
|
410 super.setToolTipText(text);
|
|
411 }
|
|
412 } else {
|
|
413 super.setToolTipText(appToolTipText);
|
|
414 }
|
|
415
|
|
416 // determine horizontal position
|
|
417 int x = rect.x + hIndent;
|
|
418 if (align_ is DWT.CENTER) {
|
|
419 x = (rect.width - extent.x)/2;
|
|
420 }
|
|
421 if (align_ is DWT.RIGHT) {
|
|
422 x = rect.width - hIndent - extent.x;
|
|
423 }
|
|
424
|
|
425 // draw a background image behind the text
|
|
426 try {
|
|
427 if (backgroundImage !is null) {
|
|
428 // draw a background image behind the text
|
|
429 Rectangle imageRect = backgroundImage.getBounds();
|
|
430 // tile image to fill space
|
|
431 gc.setBackground(getBackground());
|
|
432 gc.fillRectangle(rect);
|
|
433 int xPos = 0;
|
|
434 while (xPos < rect.width) {
|
|
435 int yPos = 0;
|
|
436 while (yPos < rect.height) {
|
|
437 gc.drawImage(backgroundImage, xPos, yPos);
|
|
438 yPos += imageRect.height;
|
|
439 }
|
|
440 xPos += imageRect.width;
|
|
441 }
|
|
442 } else if (gradientColors !is null) {
|
|
443 // draw a gradient behind the text
|
|
444 final Color oldBackground = gc.getBackground();
|
|
445 if (gradientColors.length is 1) {
|
|
446 if (gradientColors[0] !is null) gc.setBackground(gradientColors[0]);
|
|
447 gc.fillRectangle(0, 0, rect.width, rect.height);
|
|
448 } else {
|
|
449 final Color oldForeground = gc.getForeground();
|
|
450 Color lastColor = gradientColors[0];
|
|
451 if (lastColor is null) lastColor = oldBackground;
|
|
452 int pos = 0;
|
|
453 for (int i = 0; i < gradientPercents.length; ++i) {
|
|
454 gc.setForeground(lastColor);
|
|
455 lastColor = gradientColors[i + 1];
|
|
456 if (lastColor is null) lastColor = oldBackground;
|
|
457 gc.setBackground(lastColor);
|
|
458 if (gradientVertical) {
|
|
459 final int gradientHeight = (gradientPercents[i] * rect.height / 100) - pos;
|
|
460 gc.fillGradientRectangle(0, pos, rect.width, gradientHeight, true);
|
|
461 pos += gradientHeight;
|
|
462 } else {
|
|
463 final int gradientWidth = (gradientPercents[i] * rect.width / 100) - pos;
|
|
464 gc.fillGradientRectangle(pos, 0, gradientWidth, rect.height, false);
|
|
465 pos += gradientWidth;
|
|
466 }
|
|
467 }
|
|
468 if (gradientVertical && pos < rect.height) {
|
|
469 gc.setBackground(getBackground());
|
|
470 gc.fillRectangle(0, pos, rect.width, rect.height - pos);
|
|
471 }
|
|
472 if (!gradientVertical && pos < rect.width) {
|
|
473 gc.setBackground(getBackground());
|
|
474 gc.fillRectangle(pos, 0, rect.width - pos, rect.height);
|
|
475 }
|
|
476 gc.setForeground(oldForeground);
|
|
477 }
|
|
478 gc.setBackground(oldBackground);
|
|
479 } else {
|
|
480 if (background !is null || (getStyle() & DWT.DOUBLE_BUFFERED) is 0) {
|
|
481 gc.setBackground(getBackground());
|
|
482 gc.fillRectangle(rect);
|
|
483 }
|
|
484 }
|
|
485 } catch (DWTException e) {
|
|
486 if ((getStyle() & DWT.DOUBLE_BUFFERED) is 0) {
|
|
487 gc.setBackground(getBackground());
|
|
488 gc.fillRectangle(rect);
|
|
489 }
|
|
490 }
|
|
491
|
|
492 // draw border
|
|
493 int style = getStyle();
|
|
494 if ((style & DWT.SHADOW_IN) !is 0 || (style & DWT.SHADOW_OUT) !is 0) {
|
|
495 paintBorder(gc, rect);
|
|
496 }
|
|
497
|
|
498 // draw the image
|
|
499 if (img !is null) {
|
|
500 Rectangle imageRect = img.getBounds();
|
|
501 gc.drawImage(img, 0, 0, imageRect.width, imageRect.height,
|
|
502 x, (rect.height-imageRect.height)/2, imageRect.width, imageRect.height);
|
|
503 x += imageRect.width + GAP;
|
|
504 extent.x -= imageRect.width + GAP;
|
|
505 }
|
|
506 // draw the text
|
|
507 if (lines !is null) {
|
|
508 int lineHeight = gc.getFontMetrics().getHeight();
|
|
509 int textHeight = lines.length * lineHeight;
|
|
510 int lineY = Math.max(vIndent, rect.y + (rect.height - textHeight) / 2);
|
|
511 gc.setForeground(getForeground());
|
|
512 for (int i = 0; i < lines.length; i++) {
|
|
513 int lineX = x;
|
|
514 if (lines.length > 1) {
|
|
515 if (align_ is DWT.CENTER) {
|
|
516 int lineWidth = gc.textExtent(lines[i], DRAW_FLAGS).x;
|
|
517 lineX = x + Math.max(0, (extent.x - lineWidth) / 2);
|
|
518 }
|
|
519 if (align_ is DWT.RIGHT) {
|
|
520 int lineWidth = gc.textExtent(lines[i], DRAW_FLAGS).x;
|
|
521 lineX = Math.max(x, rect.x + rect.width - hIndent - lineWidth);
|
|
522 }
|
|
523 }
|
|
524 gc.drawText(lines[i], lineX, lineY, DRAW_FLAGS);
|
|
525 lineY += lineHeight;
|
|
526 }
|
|
527 }
|
|
528 }
|
|
529 /**
|
|
530 * Paint the Label's border.
|
|
531 */
|
|
532 private void paintBorder(GC gc, Rectangle r) {
|
|
533 Display disp= getDisplay();
|
|
534
|
|
535 Color c1 = null;
|
|
536 Color c2 = null;
|
|
537
|
|
538 int style = getStyle();
|
|
539 if ((style & DWT.SHADOW_IN) !is 0) {
|
|
540 c1 = disp.getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW);
|
|
541 c2 = disp.getSystemColor(DWT.COLOR_WIDGET_HIGHLIGHT_SHADOW);
|
|
542 }
|
|
543 if ((style & DWT.SHADOW_OUT) !is 0) {
|
|
544 c1 = disp.getSystemColor(DWT.COLOR_WIDGET_LIGHT_SHADOW);
|
|
545 c2 = disp.getSystemColor(DWT.COLOR_WIDGET_NORMAL_SHADOW);
|
|
546 }
|
|
547
|
|
548 if (c1 !is null && c2 !is null) {
|
|
549 gc.setLineWidth(1);
|
|
550 drawBevelRect(gc, r.x, r.y, r.width-1, r.height-1, c1, c2);
|
|
551 }
|
|
552 }
|
|
553 /**
|
|
554 * Set the alignment of the CLabel.
|
|
555 * Use the values LEFT, CENTER and RIGHT to align image and text within the available space.
|
|
556 *
|
|
557 * @param align the alignment style of LEFT, RIGHT or CENTER
|
|
558 *
|
|
559 * @exception DWTException <ul>
|
|
560 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
561 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
562 * <li>ERROR_INVALID_ARGUMENT - if the value of align is not one of DWT.LEFT, DWT.RIGHT or DWT.CENTER</li>
|
|
563 * </ul>
|
|
564 */
|
|
565 public void setAlignment(int align_) {
|
|
566 checkWidget();
|
|
567 if (align_ !is DWT.LEFT && align_ !is DWT.RIGHT && align_ !is DWT.CENTER) {
|
|
568 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
569 }
|
|
570 if (this.align_ !is align_) {
|
|
571 this.align_ = align_;
|
|
572 redraw();
|
|
573 }
|
|
574 }
|
|
575
|
|
576 public override void setBackground (Color color) {
|
|
577 super.setBackground (color);
|
|
578 // Are these settings the same as before?
|
|
579 if (backgroundImage is null &&
|
|
580 gradientColors is null &&
|
|
581 gradientPercents is null) {
|
|
582 if (color is null) {
|
|
583 if (background is null) return;
|
|
584 } else {
|
|
585 if (color ==/*eq*/ background) return;
|
|
586 }
|
|
587 }
|
|
588 background = color;
|
|
589 backgroundImage = null;
|
|
590 gradientColors = null;
|
|
591 gradientPercents = null;
|
|
592 redraw ();
|
|
593 }
|
|
594
|
|
595 /**
|
|
596 * Specify a gradient of colours to be drawn in the background of the CLabel.
|
|
597 * <p>For example, to draw a gradient that varies from dark blue to blue and then to
|
|
598 * white and stays white for the right half of the label, use the following call
|
|
599 * to setBackground:</p>
|
|
600 * <pre>
|
|
601 * clabel.setBackground(new Color[]{display.getSystemColor(DWT.COLOR_DARK_BLUE),
|
|
602 * display.getSystemColor(DWT.COLOR_BLUE),
|
|
603 * display.getSystemColor(DWT.COLOR_WHITE),
|
|
604 * display.getSystemColor(DWT.COLOR_WHITE)},
|
|
605 * new int[] {25, 50, 100});
|
|
606 * </pre>
|
|
607 *
|
|
608 * @param colors an array of Color that specifies the colors to appear in the gradient
|
|
609 * in order of appearance from left to right; The value <code>null</code>
|
|
610 * clears the background gradient; the value <code>null</code> can be used
|
|
611 * inside the array of Color to specify the background color.
|
|
612 * @param percents an array of integers between 0 and 100 specifying the percent of the width
|
|
613 * of the widget at which the color should change; the size of the percents
|
|
614 * array must be one less than the size of the colors array.
|
|
615 *
|
|
616 * @exception DWTException <ul>
|
|
617 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
618 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
619 * <li>ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistent</li>
|
|
620 * </ul>
|
|
621 */
|
|
622 public void setBackground(Color[] colors, int[] percents) {
|
|
623 setBackground(colors, percents, false);
|
|
624 }
|
|
625 /**
|
|
626 * Specify a gradient of colours to be drawn in the background of the CLabel.
|
|
627 * <p>For example, to draw a gradient that varies from dark blue to white in the vertical,
|
|
628 * direction use the following call
|
|
629 * to setBackground:</p>
|
|
630 * <pre>
|
|
631 * clabel.setBackground(new Color[]{display.getSystemColor(DWT.COLOR_DARK_BLUE),
|
|
632 * display.getSystemColor(DWT.COLOR_WHITE)},
|
|
633 * new int[] {100}, true);
|
|
634 * </pre>
|
|
635 *
|
|
636 * @param colors an array of Color that specifies the colors to appear in the gradient
|
|
637 * in order of appearance from left/top to right/bottom; The value <code>null</code>
|
|
638 * clears the background gradient; the value <code>null</code> can be used
|
|
639 * inside the array of Color to specify the background color.
|
|
640 * @param percents an array of integers between 0 and 100 specifying the percent of the width/height
|
|
641 * of the widget at which the color should change; the size of the percents
|
|
642 * array must be one less than the size of the colors array.
|
|
643 * @param vertical indicate the direction of the gradient. True is vertical and false is horizontal.
|
|
644 *
|
|
645 * @exception DWTException <ul>
|
|
646 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
647 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
648 * <li>ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistent</li>
|
|
649 * </ul>
|
|
650 *
|
|
651 * @since 3.0
|
|
652 */
|
|
653 public void setBackground(Color[] colors, int[] percents, bool vertical) {
|
|
654 checkWidget();
|
|
655 if (colors !is null) {
|
|
656 if (percents is null || percents.length !is colors.length - 1) {
|
|
657 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
658 }
|
|
659 if (getDisplay().getDepth() < 15) {
|
|
660 // Don't use gradients on low color displays
|
|
661 colors = [colors[colors.length - 1]];
|
|
662 percents = null;
|
|
663 }
|
|
664 for (int i = 0; i < percents.length; i++) {
|
|
665 if (percents[i] < 0 || percents[i] > 100) {
|
|
666 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
667 }
|
|
668 if (i > 0 && percents[i] < percents[i-1]) {
|
|
669 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
|
|
670 }
|
|
671 }
|
|
672 }
|
|
673
|
|
674 // Are these settings the same as before?
|
|
675 final Color background = getBackground();
|
|
676 if (backgroundImage is null) {
|
|
677 if ((gradientColors !is null) && (colors !is null) &&
|
|
678 (gradientColors.length is colors.length)) {
|
|
679 bool same = false;
|
|
680 for (int i = 0; i < gradientColors.length; i++) {
|
|
681 same = (gradientColors[i] is colors[i]) ||
|
|
682 ((gradientColors[i] is null) && (colors[i] is background)) ||
|
|
683 ((gradientColors[i] is background) && (colors[i] is null));
|
|
684 if (!same) break;
|
|
685 }
|
|
686 if (same) {
|
|
687 for (int i = 0; i < gradientPercents.length; i++) {
|
|
688 same = gradientPercents[i] is percents[i];
|
|
689 if (!same) break;
|
|
690 }
|
|
691 }
|
|
692 if (same && this.gradientVertical is vertical) return;
|
|
693 }
|
|
694 } else {
|
|
695 backgroundImage = null;
|
|
696 }
|
|
697 // Store the new settings
|
|
698 if (colors is null) {
|
|
699 gradientColors = null;
|
|
700 gradientPercents = null;
|
|
701 gradientVertical = false;
|
|
702 } else {
|
|
703 gradientColors = new Color[colors.length];
|
|
704 for (int i = 0; i < colors.length; ++i)
|
|
705 gradientColors[i] = (colors[i] !is null) ? colors[i] : background;
|
|
706 gradientPercents = new int[percents.length];
|
|
707 for (int i = 0; i < percents.length; ++i)
|
|
708 gradientPercents[i] = percents[i];
|
|
709 gradientVertical = vertical;
|
|
710 }
|
|
711 // Refresh with the new settings
|
|
712 redraw();
|
|
713 }
|
|
714 /**
|
|
715 * Set the image to be drawn in the background of the label.
|
|
716 *
|
|
717 * @param image the image to be drawn in the background
|
|
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 void setBackground(Image image) {
|
|
725 checkWidget();
|
|
726 if (image is backgroundImage) return;
|
|
727 if (image !is null) {
|
|
728 gradientColors = null;
|
|
729 gradientPercents = null;
|
|
730 }
|
|
731 backgroundImage = image;
|
|
732 redraw();
|
|
733
|
|
734 }
|
|
735 public override void setFont(Font font) {
|
|
736 super.setFont(font);
|
|
737 redraw();
|
|
738 }
|
|
739 /**
|
|
740 * Set the label's Image.
|
|
741 * The value <code>null</code> clears it.
|
|
742 *
|
|
743 * @param image the image to be displayed in the label or null
|
|
744 *
|
|
745 * @exception DWTException <ul>
|
|
746 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
747 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
748 * </ul>
|
|
749 */
|
|
750 public void setImage(Image image) {
|
|
751 checkWidget();
|
|
752 if (image !is this.image) {
|
|
753 this.image = image;
|
|
754 redraw();
|
|
755 }
|
|
756 }
|
|
757 /**
|
|
758 * Set the label's text.
|
|
759 * The value <code>null</code> clears it.
|
|
760 *
|
|
761 * @param text the text to be displayed in the label or null
|
|
762 *
|
|
763 * @exception DWTException <ul>
|
|
764 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
765 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
766 * </ul>
|
|
767 */
|
|
768 public void setText(char[] text) {
|
|
769 checkWidget();
|
|
770 if (text is null) text = ""; //$NON-NLS-1$
|
|
771 if ( text !=/*eq*/ this.text) {
|
|
772 this.text = text;
|
|
773 redraw();
|
|
774 }
|
|
775 }
|
|
776 public override void setToolTipText (char[] string) {
|
|
777 super.setToolTipText (string);
|
|
778 appToolTipText = super.getToolTipText();
|
|
779 }
|
|
780 /**
|
|
781 * Shorten the given text <code>t</code> so that its length doesn't exceed
|
|
782 * the given width. The default implementation replaces characters in the
|
|
783 * center of the original string with an ellipsis ("...").
|
|
784 * Override if you need a different strategy.
|
|
785 *
|
|
786 * @param gc the gc to use for text measurement
|
|
787 * @param t the text to shorten
|
|
788 * @param width the width to shorten the text to, in pixels
|
|
789 * @return the shortened text
|
|
790 */
|
|
791 protected char[] shortenText(GC gc, char[] t, int width) {
|
|
792 if (t is null) return null;
|
|
793 int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x;
|
|
794 if (width<=w) return t;
|
|
795 int l = t.length;
|
|
796 int max = l/2;
|
|
797 int min = 0;
|
|
798 int mid = (max+min)/2 - 1;
|
|
799 if (mid <= 0) return t;
|
|
800 while (min < mid && mid < max) {
|
|
801 char[] s1 = t[0 .. mid].dup;
|
|
802 char[] s2 = t[l-mid .. l].dup;
|
|
803 int l1 = gc.textExtent(s1, DRAW_FLAGS).x;
|
|
804 int l2 = gc.textExtent(s2, DRAW_FLAGS).x;
|
|
805 if (l1+w+l2 > width) {
|
|
806 max = mid;
|
|
807 mid = (max+min)/2;
|
|
808 } else if (l1+w+l2 < width) {
|
|
809 min = mid;
|
|
810 mid = (max+min)/2;
|
|
811 } else {
|
|
812 min = max;
|
|
813 }
|
|
814 }
|
|
815 if (mid is 0) return t;
|
|
816 return t[ 0 .. mid ] ~ ELLIPSIS ~ t[ l-mid .. l ];
|
|
817 }
|
|
818
|
|
819 private char[][] splitString(char[] text) {
|
|
820 char[][] lines = new char[][1];
|
|
821 int start = 0, pos;
|
|
822 do {
|
|
823 pos = tango.text.Util.locate( text, '\n', start);
|
|
824 if (pos is text.length ) {
|
|
825 lines[lines.length - 1] = text[start .. $ ];
|
|
826 } else {
|
|
827 bool crlf = (pos > 0) && (text[ pos - 1 ] is '\r');
|
|
828 lines[lines.length - 1] = text[ start .. pos - (crlf ? 1 : 0)];
|
|
829 start = pos + 1;
|
|
830 char[][] newLines = new char[][lines.length+1];
|
|
831 System.arraycopy(lines, 0, newLines, 0, lines.length);
|
|
832 lines = newLines;
|
|
833 }
|
|
834 } while (pos !is text.length);
|
|
835 return lines;
|
|
836 }
|
|
837 }
|