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