Mercurial > projects > dwt2
annotate org.eclipse.swt.gtk.linux.x86/src/org/eclipse/swt/custom/CLabel.d @ 51:c01d033c633a
[swt lin]
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 27 Mar 2009 19:58:06 +0100 |
parents | 7a2dd761a8b2 |
children | 536e43f63c81 |
rev | line source |
---|---|
25 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.custom.CLabel; | |
14 | |
15 | |
16 import org.eclipse.swt.SWT; | |
17 import org.eclipse.swt.SWTException; | |
18 import org.eclipse.swt.accessibility.ACC; | |
19 import org.eclipse.swt.accessibility.Accessible; | |
20 import org.eclipse.swt.accessibility.AccessibleAdapter; | |
21 import org.eclipse.swt.accessibility.AccessibleControlAdapter; | |
22 import org.eclipse.swt.accessibility.AccessibleControlEvent; | |
23 import org.eclipse.swt.accessibility.AccessibleEvent; | |
24 import org.eclipse.swt.events.DisposeEvent; | |
25 import org.eclipse.swt.events.DisposeListener; | |
26 import org.eclipse.swt.events.PaintEvent; | |
27 import org.eclipse.swt.events.PaintListener; | |
28 import org.eclipse.swt.events.TraverseEvent; | |
29 import org.eclipse.swt.events.TraverseListener; | |
30 import org.eclipse.swt.graphics.Color; | |
31 import org.eclipse.swt.graphics.Font; | |
32 import org.eclipse.swt.graphics.GC; | |
33 import org.eclipse.swt.graphics.Image; | |
34 import org.eclipse.swt.graphics.Point; | |
35 import org.eclipse.swt.graphics.Rectangle; | |
36 import org.eclipse.swt.graphics.TextLayout; | |
37 import org.eclipse.swt.widgets.Canvas; | |
38 import org.eclipse.swt.widgets.Composite; | |
39 import org.eclipse.swt.widgets.Control; | |
40 import org.eclipse.swt.widgets.Display; | |
41 import java.lang.all; | |
42 | |
48 | 43 version(Tango){ |
25 | 44 static import tango.text.Unicode; |
45 static import tango.text.convert.Utf; | |
48 | 46 } else { // Phobos |
47 } | |
25 | 48 |
49 /** | |
50 * A Label which supports aligned text and/or an image and different border styles. | |
51 * <p> | |
52 * If there is not enough space a CLabel uses the following strategy to fit the | |
53 * information into the available space: | |
54 * <pre> | |
55 * ignores the indent in left align mode | |
56 * ignores the image and the gap | |
57 * shortens the text by replacing the center portion of the label with an ellipsis | |
58 * shortens the text by removing the center portion of the label | |
59 * </pre> | |
60 * <p> | |
61 * <dl> | |
62 * <dt><b>Styles:</b> | |
63 * <dd>LEFT, RIGHT, CENTER, SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> | |
64 * <dt><b>Events:</b> | |
65 * <dd></dd> | |
66 * </dl> | |
67 * | |
68 * </p><p> | |
69 * IMPORTANT: This class is <em>not</em> intended to be subclassed. | |
70 * </p> | |
71 * | |
72 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: CustomControlExample</a> | |
73 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
74 */ | |
75 public class CLabel : Canvas { | |
76 | |
77 alias Canvas.computeSize computeSize; | |
78 | |
79 /** Gap between icon and text */ | |
80 private static const int GAP = 5; | |
81 /** Left and right margins */ | |
82 private static const int INDENT = 3; | |
83 /** a string inserted in the middle of text that has been shortened */ | |
84 private static const String ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026" | |
85 /** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/ | |
86 private int align_ = SWT.LEFT; | |
87 private int hIndent = INDENT; | |
88 private int vIndent = INDENT; | |
89 /** the current text */ | |
90 private String text; | |
91 /** the current icon */ | |
92 private Image image; | |
93 // The tooltip is used for two purposes - the application can set | |
94 // a tooltip or the tooltip can be used to display the full text when the | |
95 // the text has been truncated due to the label being too short. | |
96 // The appToolTip stores the tooltip set by the application. Control.tooltiptext | |
97 // contains whatever tooltip is currently being displayed. | |
98 private String appToolTipText; | |
99 | |
100 private Image backgroundImage; | |
101 private Color[] gradientColors; | |
102 private int[] gradientPercents; | |
103 private bool gradientVertical; | |
104 private Color background; | |
105 | |
106 private static int DRAW_FLAGS = SWT.DRAW_MNEMONIC | SWT.DRAW_TAB | SWT.DRAW_TRANSPARENT | SWT.DRAW_DELIMITER; | |
107 | |
108 /** | |
109 * Constructs a new instance of this class given its parent | |
110 * and a style value describing its behavior and appearance. | |
111 * <p> | |
112 * The style value is either one of the style constants defined in | |
113 * class <code>SWT</code> which is applicable to instances of this | |
114 * class, or must be built by <em>bitwise OR</em>'ing together | |
115 * (that is, using the <code>int</code> "|" operator) two or more | |
116 * of those <code>SWT</code> style constants. The class description | |
117 * lists the style constants that are applicable to the class. | |
118 * Style bits are also inherited from superclasses. | |
119 * </p> | |
120 * | |
121 * @param parent a widget which will be the parent of the new instance (cannot be null) | |
122 * @param style the style of widget to construct | |
123 * | |
124 * @exception IllegalArgumentException <ul> | |
125 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
126 * </ul> | |
127 * @exception SWTException <ul> | |
128 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
129 * </ul> | |
130 * | |
131 * @see SWT#LEFT | |
132 * @see SWT#RIGHT | |
133 * @see SWT#CENTER | |
134 * @see SWT#SHADOW_IN | |
135 * @see SWT#SHADOW_OUT | |
136 * @see SWT#SHADOW_NONE | |
137 * @see #getStyle() | |
138 */ | |
139 public this(Composite parent, int style) { | |
140 super(parent, checkStyle(style)); | |
141 if ((style & (SWT.CENTER | SWT.RIGHT)) is 0) style |= SWT.LEFT; | |
142 if ((style & SWT.CENTER) !is 0) align_ = SWT.CENTER; | |
143 if ((style & SWT.RIGHT) !is 0) align_ = SWT.RIGHT; | |
144 if ((style & SWT.LEFT) !is 0) align_ = SWT.LEFT; | |
145 | |
146 addPaintListener(new class() PaintListener{ | |
147 public void paintControl(PaintEvent event) { | |
148 onPaint(event); | |
149 } | |
150 }); | |
151 | |
152 addDisposeListener(new class() DisposeListener{ | |
153 public void widgetDisposed(DisposeEvent event) { | |
154 onDispose(event); | |
155 } | |
156 }); | |
157 | |
158 addTraverseListener(new class() TraverseListener { | |
159 public void keyTraversed(TraverseEvent event) { | |
160 if (event.detail is SWT.TRAVERSE_MNEMONIC) { | |
161 onMnemonic(event); | |
162 } | |
163 } | |
164 }); | |
165 | |
166 initAccessible(); | |
167 | |
168 } | |
169 /** | |
170 * Check the style bits to ensure that no invalid styles are applied. | |
171 */ | |
172 private static int checkStyle (int style) { | |
173 if ((style & SWT.BORDER) !is 0) style |= SWT.SHADOW_IN; | |
174 int mask = SWT.SHADOW_IN | SWT.SHADOW_OUT | SWT.SHADOW_NONE | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; | |
175 style = style & mask; | |
176 return style |= SWT.NO_FOCUS | SWT.DOUBLE_BUFFERED; | |
177 } | |
178 | |
179 //protected void checkSubclass () { | |
180 // String name = getClass().getName (); | |
181 // String validName = CLabel.class.getName(); | |
182 // if (!validName.equals(name)) { | |
183 // SWT.error (SWT.ERROR_INVALID_SUBCLASS); | |
184 // } | |
185 //} | |
186 | |
187 public override Point computeSize(int wHint, int hHint, bool changed) { | |
188 checkWidget(); | |
189 Point e = getTotalSize(image, text); | |
190 if (wHint is SWT.DEFAULT){ | |
191 e.x += 2*hIndent; | |
192 } else { | |
193 e.x = wHint; | |
194 } | |
195 if (hHint is SWT.DEFAULT) { | |
196 e.y += 2*vIndent; | |
197 } else { | |
198 e.y = hHint; | |
199 } | |
200 return e; | |
201 } | |
202 /** | |
203 * Draw a rectangle in the given colors. | |
204 */ | |
205 private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) { | |
206 gc.setForeground(bottomright); | |
207 gc.drawLine(x+w, y, x+w, y+h); | |
208 gc.drawLine(x, y+h, x+w, y+h); | |
209 | |
210 gc.setForeground(topleft); | |
211 gc.drawLine(x, y, x+w-1, y); | |
212 gc.drawLine(x, y, x, y+h-1); | |
213 } | |
214 /* | |
215 * Return the lowercase of the first non-'&' character following | |
216 * an '&' character in the given string. If there are no '&' | |
217 * characters in the given string, return '\0'. | |
218 */ | |
219 dchar _findMnemonic (String string) { | |
220 if (string is null) return '\0'; | |
221 int index = 0; | |
222 int length = string.length; | |
223 do { | |
224 while (index < length && string[index] !is '&') index++; | |
225 if (++index >= length) return '\0'; | |
51 | 226 if (string[index] !is '&') return Character.toLowerCase (string.dcharAt (index)); |
25 | 227 index++; |
228 } while (index < length); | |
229 return '\0'; | |
230 } | |
231 /** | |
232 * Returns the alignment. | |
233 * The alignment style (LEFT, CENTER or RIGHT) is returned. | |
234 * | |
235 * @return SWT.LEFT, SWT.RIGHT or SWT.CENTER | |
236 */ | |
237 public int getAlignment() { | |
238 //checkWidget(); | |
239 return align_; | |
240 } | |
241 /** | |
242 * Return the CLabel's image or <code>null</code>. | |
243 * | |
244 * @return the image of the label or null | |
245 */ | |
246 public Image getImage() { | |
247 //checkWidget(); | |
248 return image; | |
249 } | |
250 /** | |
251 * Compute the minimum size. | |
252 */ | |
253 private Point getTotalSize(Image image, String text) { | |
254 Point size = new Point(0, 0); | |
255 | |
256 if (image !is null) { | |
257 Rectangle r = image.getBounds(); | |
258 size.x += r.width; | |
259 size.y += r.height; | |
260 } | |
261 | |
262 GC gc = new GC(this); | |
263 if (text !is null && text.length > 0) { | |
264 Point e = gc.textExtent(text, DRAW_FLAGS); | |
265 size.x += e.x; | |
266 size.y = Math.max(size.y, e.y); | |
267 if (image !is null) size.x += GAP; | |
268 } else { | |
269 size.y = Math.max(size.y, gc.getFontMetrics().getHeight()); | |
270 } | |
271 gc.dispose(); | |
272 | |
273 return size; | |
274 } | |
275 public override int getStyle () { | |
276 int style = super.getStyle(); | |
277 switch (align_) { | |
278 case SWT.RIGHT: style |= SWT.RIGHT; break; | |
279 case SWT.CENTER: style |= SWT.CENTER; break; | |
280 case SWT.LEFT: style |= SWT.LEFT; break; | |
281 default: | |
282 } | |
283 return style; | |
284 } | |
285 | |
286 /** | |
287 * Return the Label's text. | |
288 * | |
289 * @return the text of the label or null | |
290 */ | |
291 public String getText() { | |
292 //checkWidget(); | |
293 return text; | |
294 } | |
295 public override String getToolTipText () { | |
296 checkWidget(); | |
297 return appToolTipText; | |
298 } | |
299 private void initAccessible() { | |
300 Accessible accessible = getAccessible(); | |
301 accessible.addAccessibleListener(new class() AccessibleAdapter { | |
302 public void getName(AccessibleEvent e) { | |
303 e.result = getText(); | |
304 } | |
305 | |
306 public void getHelp(AccessibleEvent e) { | |
307 e.result = getToolTipText(); | |
308 } | |
309 | |
310 public void getKeyboardShortcut(AccessibleEvent e) { | |
311 dchar mnemonic = _findMnemonic(this.outer.text); | |
312 if (mnemonic !is '\0') { | |
313 dchar[1] d; | |
314 d[0] = mnemonic; | |
51 | 315 e.result = Format("Alt+{}", d); //$NON-NLS-1$ |
25 | 316 } |
317 } | |
318 }); | |
319 | |
320 accessible.addAccessibleControlListener(new class() AccessibleControlAdapter { | |
321 public void getChildAtPoint(AccessibleControlEvent e) { | |
322 e.childID = ACC.CHILDID_SELF; | |
323 } | |
324 | |
325 public void getLocation(AccessibleControlEvent e) { | |
326 Rectangle rect = getDisplay().map(getParent(), null, getBounds()); | |
327 e.x = rect.x; | |
328 e.y = rect.y; | |
329 e.width = rect.width; | |
330 e.height = rect.height; | |
331 } | |
332 | |
333 public void getChildCount(AccessibleControlEvent e) { | |
334 e.detail = 0; | |
335 } | |
336 | |
337 public void getRole(AccessibleControlEvent e) { | |
338 e.detail = ACC.ROLE_LABEL; | |
339 } | |
340 | |
341 public void getState(AccessibleControlEvent e) { | |
342 e.detail = ACC.STATE_READONLY; | |
343 } | |
344 }); | |
345 } | |
346 void onDispose(DisposeEvent event) { | |
347 gradientColors = null; | |
348 gradientPercents = null; | |
349 backgroundImage = null; | |
350 text = null; | |
351 image = null; | |
352 appToolTipText = null; | |
353 } | |
354 void onMnemonic(TraverseEvent event) { | |
355 dchar mnemonic = _findMnemonic(text); | |
356 if (mnemonic is '\0') return; | |
51 | 357 if (Character.toLowerCase(event.character) !is mnemonic) return; |
25 | 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 = SWT.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 String 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 String[] 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 SWT.CENTER) { | |
419 x = (rect.width - extent.x)/2; | |
420 } | |
421 if (align_ is SWT.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 | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
444 Color oldBackground = gc.getBackground(); |
25 | 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 { | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
449 Color oldForeground = gc.getForeground(); |
25 | 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) { | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
459 int gradientHeight = (gradientPercents[i] * rect.height / 100) - pos; |
25 | 460 gc.fillGradientRectangle(0, pos, rect.width, gradientHeight, true); |
461 pos += gradientHeight; | |
462 } else { | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
463 int gradientWidth = (gradientPercents[i] * rect.width / 100) - pos; |
25 | 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() & SWT.DOUBLE_BUFFERED) is 0) { | |
481 gc.setBackground(getBackground()); | |
482 gc.fillRectangle(rect); | |
483 } | |
484 } | |
485 } catch (SWTException e) { | |
486 if ((getStyle() & SWT.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 & SWT.SHADOW_IN) !is 0 || (style & SWT.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 SWT.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 SWT.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 & SWT.SHADOW_IN) !is 0) { | |
540 c1 = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); | |
541 c2 = disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW); | |
542 } | |
543 if ((style & SWT.SHADOW_OUT) !is 0) { | |
544 c1 = disp.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW); | |
545 c2 = disp.getSystemColor(SWT.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 SWTException <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 SWT.LEFT, SWT.RIGHT or SWT.CENTER</li> | |
563 * </ul> | |
564 */ | |
565 public void setAlignment(int align_) { | |
566 checkWidget(); | |
567 if (align_ !is SWT.LEFT && align_ !is SWT.RIGHT && align_ !is SWT.CENTER) { | |
568 SWT.error(SWT.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(SWT.COLOR_DARK_BLUE), | |
602 * display.getSystemColor(SWT.COLOR_BLUE), | |
603 * display.getSystemColor(SWT.COLOR_WHITE), | |
604 * display.getSystemColor(SWT.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 SWTException <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(SWT.COLOR_DARK_BLUE), | |
632 * display.getSystemColor(SWT.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 SWTException <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 SWT.error(SWT.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 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
667 } | |
668 if (i > 0 && percents[i] < percents[i-1]) { | |
669 SWT.error(SWT.ERROR_INVALID_ARGUMENT); | |
670 } | |
671 } | |
672 } | |
673 | |
674 // Are these settings the same as before? | |
49
7a2dd761a8b2
more work until dmd 2.026 linux segfaults.
Frank Benoit <benoit@tionex.de>
parents:
48
diff
changeset
|
675 Color background = getBackground(); |
25 | 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 SWTException <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 SWTException <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 SWTException <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(String 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 (String 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 String shortenText(GC gc, String 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 TextLayout layout = new TextLayout (getDisplay()); | |
801 layout.setText(t); | |
802 mid = validateOffset(layout, mid); | |
803 while (min < mid && mid < max) { | |
51 | 804 String s1 = t.substring(0, mid); |
25 | 805 String s2 = t.substring(validateOffset(layout, l-mid), l); |
806 int l1 = gc.textExtent(s1, DRAW_FLAGS).x; | |
807 int l2 = gc.textExtent(s2, DRAW_FLAGS).x; | |
808 if (l1+w+l2 > width) { | |
809 max = mid; | |
810 mid = validateOffset(layout, (max+min)/2); | |
811 } else if (l1+w+l2 < width) { | |
812 min = mid; | |
813 mid = validateOffset(layout, (max+min)/2); | |
814 } else { | |
815 min = max; | |
816 } | |
817 } | |
818 String result = mid is 0 ? t : t.substring(0, mid) ~ ELLIPSIS ~ t.substring(validateOffset(layout, l-mid), l); | |
819 layout.dispose(); | |
820 return result; | |
821 } | |
822 int validateOffset(TextLayout layout, int offset) { | |
823 int nextOffset = layout.getNextOffset(offset, SWT.MOVEMENT_CLUSTER); | |
824 if (nextOffset !is offset) return layout.getPreviousOffset(nextOffset, SWT.MOVEMENT_CLUSTER); | |
825 return offset; | |
826 } | |
827 private String[] splitString(String text) { | |
828 String[] lines = new String[1]; | |
829 int start = 0, pos; | |
830 do { | |
51 | 831 pos = text.indexOf( '\n', start); |
832 if (pos is -1 ) { | |
25 | 833 lines[lines.length - 1] = text[start .. $ ]; |
834 } else { | |
835 bool crlf = (pos > 0) && (text[ pos - 1 ] is '\r'); | |
836 lines[lines.length - 1] = text[ start .. pos - (crlf ? 1 : 0)]; | |
837 start = pos + 1; | |
838 String[] newLines = new String[lines.length+1]; | |
839 System.arraycopy(lines, 0, newLines, 0, lines.length); | |
840 lines = newLines; | |
841 } | |
842 } while (pos !is text.length); | |
843 return lines; | |
844 } | |
845 } |