comparison dwtx/draw2d/Label.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children c3583c6ec027
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.Label;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.graphics.Font;
20 import dwt.graphics.Image;
21 import dwtx.draw2d.geometry.Dimension;
22 import dwtx.draw2d.geometry.Insets;
23 import dwtx.draw2d.geometry.Point;
24 import dwtx.draw2d.geometry.Rectangle;
25 import dwtx.draw2d.Figure;
26 import dwtx.draw2d.PositionConstants;
27 import dwtx.draw2d.IFigure;
28 import dwtx.draw2d.Graphics;
29 import dwtx.draw2d.TextUtilities;
30 import dwtx.draw2d.ColorConstants;
31
32 /**
33 * A figure that can display text and/or an image.
34 */
35 public class Label
36 : Figure
37 , PositionConstants
38 {
39 alias Figure.getPreferredSize getPreferredSize;
40
41 private static String ELLIPSIS = "..."; //$NON-NLS-1$
42
43
44 private Image icon;
45 private String text = "";//$NON-NLS-1$
46 private String subStringText;
47 private Dimension textSize;
48 private Dimension subStringTextSize;
49 private Dimension iconSize;
50 private Point iconLocation;
51 private Point textLocation;
52 private int textAlignment = CENTER;
53 private int iconAlignment = CENTER;
54 private int labelAlignment = CENTER;
55 private int textPlacement = EAST;
56 private int iconTextGap = 3;
57
58 /**
59 * Construct an empty Label.
60 *
61 * @since 2.0
62 */
63 public this() {
64 iconSize = new Dimension(0, 0);
65 }
66
67 /**
68 * Construct a Label with passed String as its text.
69 *
70 * @param s the label text
71 * @since 2.0
72 */
73 public this(String s) {
74 this();
75 setText(s);
76 }
77
78 /**
79 * Construct a Label with passed Image as its icon.
80 *
81 * @param i the label image
82 * @since 2.0
83 */
84 public this(Image i) {
85 this();
86 setIcon(i);
87 }
88
89 /**
90 * Construct a Label with passed String as text and passed Image as its icon.
91 *
92 * @param s the label text
93 * @param i the label image
94 * @since 2.0
95 */
96 public this(String s, Image i) {
97 this();
98 setText(s);
99 setIcon(i);
100 }
101
102 private void alignOnHeight(Point loc, Dimension size, int alignment) {
103 Insets insets = getInsets();
104 switch(alignment) {
105 case TOP:
106 loc.y = insets.top;
107 break;
108 case BOTTOM:
109 loc.y = bounds.height - size.height - insets.bottom;
110 break;
111 default:
112 loc.y = (bounds.height - size.height) / 2;
113 }
114 }
115
116 private void alignOnWidth(Point loc, Dimension size, int alignment) {
117 Insets insets = getInsets();
118 switch(alignment) {
119 case LEFT:
120 loc.x = insets.left;
121 break;
122 case RIGHT:
123 loc.x = bounds.width - size.width - insets.right;
124 break;
125 default:
126 loc.x = (bounds.width - size.width) / 2;
127 }
128 }
129
130 private void calculateAlignment() {
131 switch(textPlacement) {
132 case EAST:
133 case WEST:
134 alignOnHeight(textLocation, getTextSize(), textAlignment);
135 alignOnHeight(iconLocation, getIconSize(), iconAlignment);
136 break;
137 case NORTH:
138 case SOUTH:
139 alignOnWidth(textLocation, getSubStringTextSize(), textAlignment);
140 alignOnWidth(iconLocation, getIconSize(), iconAlignment);
141 break;
142 }
143 }
144
145 /**
146 * Calculates the size of the Label using the passed Dimension as the size of the Label's
147 * text.
148 *
149 * @param txtSize the precalculated size of the label's text
150 * @return the label's size
151 * @since 2.0
152 */
153 protected Dimension calculateLabelSize(Dimension txtSize) {
154 int gap = getIconTextGap();
155 if (getIcon() is null || getText().equals("")) //$NON-NLS-1$
156 gap = 0;
157 Dimension d = new Dimension(0, 0);
158 if (textPlacement is WEST || textPlacement is EAST) {
159 d.width = getIconSize().width + gap + txtSize.width;
160 d.height = Math.max(getIconSize().height, txtSize.height);
161 } else {
162 d.width = Math.max(getIconSize().width, txtSize.width);
163 d.height = getIconSize().height + gap + txtSize.height;
164 }
165 return d;
166 }
167
168 private void calculateLocations() {
169 textLocation = new Point();
170 iconLocation = new Point();
171
172 calculatePlacement();
173 calculateAlignment();
174 Dimension offset = getSize().getDifference(getPreferredSize());
175 offset.width += getTextSize().width - getSubStringTextSize().width;
176 switch (labelAlignment) {
177 case CENTER:
178 offset.scale(0.5f);
179 break;
180 case LEFT:
181 offset.scale(0.0f);
182 break;
183 case RIGHT:
184 offset.scale(1.0f);
185 break;
186 case TOP:
187 offset.height = 0;
188 offset.scale(0.5f);
189 break;
190 case BOTTOM:
191 offset.height = offset.height * 2;
192 offset.scale(0.5f);
193 break;
194 default:
195 offset.scale(0.5f);
196 break;
197 }
198
199 switch (textPlacement) {
200 case EAST:
201 case WEST:
202 offset.height = 0;
203 break;
204 case NORTH:
205 case SOUTH:
206 offset.width = 0;
207 break;
208 }
209
210 textLocation.translate(offset);
211 iconLocation.translate(offset);
212 }
213
214 private void calculatePlacement() {
215 int gap = getIconTextGap();
216 if (icon is null || text.equals("")) //$NON-NLS-1$
217 gap = 0;
218 Insets insets = getInsets();
219
220 switch(textPlacement) {
221 case EAST:
222 iconLocation.x = insets.left;
223 textLocation.x = getIconSize().width + gap + insets.left;
224 break;
225 case WEST:
226 textLocation.x = insets.left;
227 iconLocation.x = getSubStringTextSize().width + gap + insets.left;
228 break;
229 case NORTH:
230 textLocation.y = insets.top;
231 iconLocation.y = getTextSize().height + gap + insets.top;
232 break;
233 case SOUTH:
234 textLocation.y = getIconSize().height + gap + insets.top;
235 iconLocation.y = insets.top;
236 }
237 }
238
239 /**
240 * Calculates the size of the Label's text size. The text size calculated takes into
241 * consideration if the Label's text is currently truncated. If text size without
242 * considering current truncation is desired, use {@link #calculateTextSize()}.
243 *
244 * @return the size of the label's text, taking into account truncation
245 * @since 2.0
246 */
247 protected Dimension calculateSubStringTextSize() {
248 return getTextUtilities().getTextExtents(getSubStringText(), getFont());
249 }
250
251 /**
252 * Calculates and returns the size of the Label's text. Note that this Dimension is
253 * calculated using the Label's full text, regardless of whether or not its text is
254 * currently truncated. If text size considering current truncation is desired, use
255 * {@link #calculateSubStringTextSize()}.
256 *
257 * @return the size of the label's text, ignoring truncation
258 * @since 2.0
259 */
260 protected Dimension calculateTextSize() {
261 return getTextUtilities().getTextExtents(getText(), getFont());
262 }
263
264 private void clearLocations() {
265 iconLocation = textLocation = null;
266 }
267
268 /**
269 * Returns the Label's icon.
270 *
271 * @return the label icon
272 * @since 2.0
273 */
274 public Image getIcon() {
275 return icon;
276 }
277
278 /**
279 * Returns the current alignment of the Label's icon. The default is
280 * {@link PositionConstants#CENTER}.
281 *
282 * @return the icon alignment
283 * @since 2.0
284 */
285 public int getIconAlignment() {
286 return iconAlignment;
287 }
288
289 /**
290 * Returns the bounds of the Label's icon.
291 *
292 * @return the icon's bounds
293 * @since 2.0
294 */
295 public Rectangle getIconBounds() {
296 Rectangle bounds = getBounds();
297 return new Rectangle(bounds.getLocation().translate(getIconLocation()), getIconSize());
298 }
299
300 /**
301 * Returns the location of the Label's icon relative to the Label.
302 *
303 * @return the icon's location
304 * @since 2.0
305 */
306 protected Point getIconLocation() {
307 if (iconLocation is null)
308 calculateLocations();
309 return iconLocation;
310 }
311
312 /**
313 * Returns the gap in pixels between the Label's icon and its text.
314 *
315 * @return the gap
316 * @since 2.0
317 */
318 public int getIconTextGap() {
319 return iconTextGap;
320 }
321
322 /**
323 * @see IFigure#getMinimumSize(int, int)
324 */
325 public Dimension getMinimumSize(int w, int h) {
326 if (minSize !is null)
327 return minSize;
328 minSize = new Dimension();
329 if (getLayoutManager() !is null)
330 minSize.setSize(getLayoutManager().getMinimumSize(this, w, h));
331
332 Dimension labelSize =
333 calculateLabelSize(getTextUtilities().getTextExtents(getTruncationString(), getFont())
334 .intersect(getTextUtilities().getTextExtents(getText(), getFont())));
335 Insets insets = getInsets();
336 labelSize.expand(insets.getWidth(), insets.getHeight());
337 minSize.union_(labelSize);
338 return minSize;
339 }
340
341 /**
342 * @see IFigure#getPreferredSize(int, int)
343 */
344 public Dimension getPreferredSize(int wHint, int hHint) {
345 if (prefSize is null) {
346 prefSize = calculateLabelSize(getTextSize());
347 Insets insets = getInsets();
348 prefSize.expand(insets.getWidth(), insets.getHeight());
349 if (getLayoutManager() !is null)
350 prefSize.union_(getLayoutManager().getPreferredSize(this, wHint, hHint));
351 }
352 if (wHint >= 0 && wHint < prefSize.width) {
353 Dimension minSize = getMinimumSize(wHint, hHint);
354 Dimension result = prefSize.getCopy();
355 result.width = Math.min(result.width, wHint);
356 result.width = Math.max(minSize.width, result.width);
357 return result;
358 }
359 return prefSize;
360 }
361
362 /**
363 * Calculates the amount of the Label's current text will fit in the Label, including an
364 * elipsis "..." if truncation is required.
365 *
366 * @return the substring
367 * @since 2.0
368 */
369 public String getSubStringText() {
370 if (subStringText !is null)
371 return subStringText;
372
373 subStringText = text;
374 int widthShrink = getPreferredSize().width - getSize().width;
375 if (widthShrink <= 0)
376 return subStringText;
377
378 Dimension effectiveSize = getTextSize().getExpanded(-widthShrink, 0);
379 Font currentFont = getFont();
380 int dotsWidth = getTextUtilities().getTextExtents(getTruncationString(), currentFont).width;
381
382 if (effectiveSize.width < dotsWidth)
383 effectiveSize.width = dotsWidth;
384
385 int subStringLength = getTextUtilities().getLargestSubstringConfinedTo(text,
386 currentFont,
387 effectiveSize.width - dotsWidth);
388 subStringText = (text.substring(0, subStringLength) ~ getTruncationString()).dup;
389 return subStringText;
390 }
391
392 /**
393 * Returns the size of the Label's current text. If the text is currently truncated, the
394 * truncated text with its ellipsis is used to calculate the size.
395 *
396 * @return the size of this label's text, taking into account truncation
397 * @since 2.0
398 */
399 protected Dimension getSubStringTextSize() {
400 if (subStringTextSize is null)
401 subStringTextSize = calculateSubStringTextSize();
402 return subStringTextSize;
403 }
404
405 /**
406 * Returns the text of the label. Note that this is the complete text of the label,
407 * regardless of whether it is currently being truncated. Call {@link #getSubStringText()}
408 * to return the label's current text contents with truncation considered.
409 *
410 * @return the complete text of this label
411 * @since 2.0
412 */
413 public String getText() {
414 return text;
415 }
416
417 /**
418 * Returns the current alignment of the Label's text. The default text alignment is
419 * {@link PositionConstants#CENTER}.
420 *
421 * @return the text alignment
422 */
423 public int getTextAlignment() {
424 return textAlignment;
425 }
426
427 /**
428 * Returns the bounds of the label's text. Note that the bounds are calculated using the
429 * label's complete text regardless of whether the label's text is currently truncated.
430 *
431 * @return the bounds of this label's complete text
432 * @since 2.0
433 */
434 public Rectangle getTextBounds() {
435 Rectangle bounds = getBounds();
436 return new Rectangle(bounds.getLocation().translate(getTextLocation()), textSize);
437 }
438
439 /**
440 * Returns the location of the label's text relative to the label.
441 *
442 * @return the text location
443 * @since 2.0
444 */
445 protected Point getTextLocation() {
446 if (textLocation !is null)
447 return textLocation;
448 calculateLocations();
449 return textLocation;
450 }
451
452 /**
453 * Returns the current placement of the label's text relative to its icon. The default
454 * text placement is {@link PositionConstants#EAST}.
455 *
456 * @return the text placement
457 * @since 2.0
458 */
459 public int getTextPlacement() {
460 return textPlacement;
461 }
462
463 /**
464 * Returns the size of the label's complete text. Note that the text used to make this
465 * calculation is the label's full text, regardless of whether the label's text is
466 * currently being truncated and is displaying an ellipsis. If the size considering
467 * current truncation is desired, call {@link #getSubStringTextSize()}.
468 *
469 * @return the size of this label's complete text
470 * @since 2.0
471 */
472 protected Dimension getTextSize() {
473 if (textSize is null)
474 textSize = calculateTextSize();
475 return textSize;
476 }
477
478 /**
479 * @see IFigure#invalidate()
480 */
481 public void invalidate() {
482 prefSize = null;
483 minSize = null;
484 clearLocations();
485 textSize = null;
486 subStringTextSize = null;
487 subStringText = null;
488 super.invalidate();
489 }
490
491 /**
492 * Returns <code>true</code> if the label's text is currently truncated and is displaying
493 * an ellipsis, <code>false</code> otherwise.
494 *
495 * @return <code>true</code> if the label's text is truncated
496 * @since 2.0
497 */
498 public bool isTextTruncated() {
499 return !getSubStringText().equals(getText());
500 }
501
502 /**
503 * @see Figure#paintFigure(Graphics)
504 */
505 protected void paintFigure(Graphics graphics) {
506 if (isOpaque())
507 super.paintFigure(graphics);
508 Rectangle bounds = getBounds();
509 graphics.translate(bounds.x, bounds.y);
510 if (icon !is null)
511 graphics.drawImage(icon, getIconLocation());
512 if (!isEnabled()) {
513 graphics.translate(1, 1);
514 graphics.setForegroundColor(ColorConstants.buttonLightest);
515 graphics.drawText(getSubStringText(), getTextLocation());
516 graphics.translate(-1, -1);
517 graphics.setForegroundColor(ColorConstants.buttonDarker);
518 }
519 graphics.drawText(getSubStringText(), getTextLocation());
520 graphics.translate(-bounds.x, -bounds.y);
521 }
522
523 /**
524 * Sets the label's icon to the passed image.
525 *
526 * @param image the new label image
527 * @since 2.0
528 */
529 public void setIcon(Image image) {
530 if (icon is image)
531 return;
532 icon = image;
533 //Call repaint, in case the image dimensions are the same.
534 repaint();
535 if (icon is null)
536 setIconDimension(new Dimension());
537 else
538 setIconDimension(new Dimension(image));
539 }
540
541 /**
542 * This method sets the alignment of the icon within the bounds of the label. If the label
543 * is larger than the icon, then the icon will be aligned according to this alignment.
544 * Valid values are:
545 * <UL>
546 * <LI><EM>{@link PositionConstants#CENTER}</EM>
547 * <LI>{@link PositionConstants#TOP}
548 * <LI>{@link PositionConstants#BOTTOM}
549 * <LI>{@link PositionConstants#LEFT}
550 * <LI>{@link PositionConstants#RIGHT}
551 * </UL>
552 * @param align the icon alignment
553 * @since 2.0
554 */
555 public void setIconAlignment (int align_) {
556 if (iconAlignment is align_)
557 return;
558 iconAlignment = align_;
559 clearLocations();
560 repaint();
561 }
562
563 /**
564 * Sets the label's icon size to the passed Dimension.
565 *
566 * @param d the new icon size
567 * @deprecated the icon is automatically displayed at 1:1
568 * @since 2.0
569 */
570 public void setIconDimension(Dimension d) {
571 if (d.opEquals(getIconSize()))
572 return;
573 iconSize = d;
574 revalidate();
575 }
576
577 /**
578 * Sets the gap in pixels between the label's icon and text to the passed value. The
579 * default is 4.
580 *
581 * @param gap the gap
582 * @since 2.0
583 */
584 public void setIconTextGap(int gap) {
585 if (iconTextGap is gap)
586 return;
587 iconTextGap = gap;
588 repaint();
589 revalidate();
590 }
591
592 /**
593 * Sets the alignment of the label (icon and text) within the figure. If this
594 * figure's bounds are larger than the size needed to display the label, the
595 * label will be aligned accordingly. Valid values are:
596 * <UL>
597 * <LI><EM>{@link PositionConstants#CENTER}</EM>
598 * <LI>{@link PositionConstants#TOP}
599 * <LI>{@link PositionConstants#BOTTOM}
600 * <LI>{@link PositionConstants#LEFT}
601 * <LI>{@link PositionConstants#RIGHT}
602 * </UL>
603 *
604 * @param align label alignment
605 */
606 public void setLabelAlignment(int align_) {
607 if (labelAlignment is align_)
608 return;
609 labelAlignment = align_;
610 clearLocations();
611 repaint();
612 }
613
614 /**
615 * Sets the label's text.
616 * @param s the new label text
617 * @since 2.0
618 */
619 public void setText(String s) {
620 //"text" will never be null.
621 if (s is null)
622 s = "";//$NON-NLS-1$
623 if (text.equals(s))
624 return;
625 text = s;
626 revalidate();
627 repaint();
628 }
629
630 /**
631 * Sets the alignment of the text relative to the icon within the label. The text
632 * alignment must be orthogonal to the text placement. For example, if the placement
633 * is EAST, then the text can be aligned using TOP, CENTER, or BOTTOM. Valid values are:
634 * <UL>
635 * <LI><EM>{@link PositionConstants#CENTER}</EM>
636 * <LI>{@link PositionConstants#TOP}
637 * <LI>{@link PositionConstants#BOTTOM}
638 * <LI>{@link PositionConstants#LEFT}
639 * <LI>{@link PositionConstants#RIGHT}
640 * </UL>
641 * @see #setLabelAlignment(int)
642 * @param align the text alignment
643 * @since 2.0
644 */
645 public void setTextAlignment(int align_) {
646 if (textAlignment is align_)
647 return;
648 textAlignment = align_;
649 clearLocations();
650 repaint();
651 }
652
653 /**
654 * Sets the placement of the text relative to the icon within the label.
655 * Valid values are:
656 * <UL>
657 * <LI><EM>{@link PositionConstants#EAST}</EM>
658 * <LI>{@link PositionConstants#NORTH}
659 * <LI>{@link PositionConstants#SOUTH}
660 * <LI>{@link PositionConstants#WEST}
661 * </UL>
662 *
663 * @param where the text placement
664 * @since 2.0
665 */
666 public void setTextPlacement (int where) {
667 if (textPlacement is where)
668 return;
669 textPlacement = where;
670 revalidate();
671 repaint();
672 }
673
674 /**
675 * Gets the <code>TextUtilities</code> instance to be used in measurement
676 * calculations.
677 *
678 * @return a <code>TextUtilities</code> instance
679 * @since 3.4
680 */
681 public TextUtilities getTextUtilities() {
682 return TextUtilities.INSTANCE;
683 }
684
685 /**
686 * Gets the string that will be appended to the text when the label is
687 * truncated. By default, this returns an ellipsis.
688 *
689 * @return the string to append to the text when truncated
690 * @since 3.4
691 */
692 protected String getTruncationString() {
693 return ELLIPSIS;
694 }
695
696 /**
697 * Gets the icon size
698 *
699 * @return the icon size
700 * @since 3.4
701 */
702 protected Dimension getIconSize() {
703 return iconSize;
704 }
705
706 }