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