comparison dwt/custom/CLabel.d @ 212:ab60f3309436

reverted the char[] to String and use the an alias.
author Frank Benoit <benoit@tionex.de>
date Mon, 05 May 2008 00:12:38 +0200
parents a5afe31f5cdd
children 36f5cb12e1a2
comparison
equal deleted inserted replaced
211:ff59aeb96cac 212:ab60f3309436
72 /** Gap between icon and text */ 72 /** Gap between icon and text */
73 private static const int GAP = 5; 73 private static const int GAP = 5;
74 /** Left and right margins */ 74 /** Left and right margins */
75 private static const int INDENT = 3; 75 private static const int INDENT = 3;
76 /** a string inserted in the middle of text that has been shortened */ 76 /** a string inserted in the middle of text that has been shortened */
77 private static const char[] ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026" 77 private static const String ELLIPSIS = "..."; //$NON-NLS-1$ // could use the ellipsis glyph on some platforms "\u2026"
78 /** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/ 78 /** the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT*/
79 private int align_ = DWT.LEFT; 79 private int align_ = DWT.LEFT;
80 private int hIndent = INDENT; 80 private int hIndent = INDENT;
81 private int vIndent = INDENT; 81 private int vIndent = INDENT;
82 /** the current text */ 82 /** the current text */
83 private char[] text; 83 private String text;
84 /** the current icon */ 84 /** the current icon */
85 private Image image; 85 private Image image;
86 // The tooltip is used for two purposes - the application can set 86 // The tooltip is used for two purposes - the application can set
87 // a tooltip or the tooltip can be used to display the full text when the 87 // a tooltip or the tooltip can be used to display the full text when the
88 // the text has been truncated due to the label being too short. 88 // the text has been truncated due to the label being too short.
89 // The appToolTip stores the tooltip set by the application. Control.tooltiptext 89 // The appToolTip stores the tooltip set by the application. Control.tooltiptext
90 // contains whatever tooltip is currently being displayed. 90 // contains whatever tooltip is currently being displayed.
91 private char[] appToolTipText; 91 private String appToolTipText;
92 92
93 private Image backgroundImage; 93 private Image backgroundImage;
94 private Color[] gradientColors; 94 private Color[] gradientColors;
95 private int[] gradientPercents; 95 private int[] gradientPercents;
96 private bool gradientVertical; 96 private bool gradientVertical;
207 /* 207 /*
208 * Return the lowercase of the first non-'&' character following 208 * Return the lowercase of the first non-'&' character following
209 * an '&' character in the given string. If there are no '&' 209 * an '&' character in the given string. If there are no '&'
210 * characters in the given string, return '\0'. 210 * characters in the given string, return '\0'.
211 */ 211 */
212 dchar _findMnemonic (char[] string) { 212 dchar _findMnemonic (String string) {
213 if (string is null) return '\0'; 213 if (string is null) return '\0';
214 int index = 0; 214 int index = 0;
215 int length = string.length; 215 int length = string.length;
216 do { 216 do {
217 while (index < length && string[index] !is '&') index++; 217 while (index < length && string[index] !is '&') index++;
246 return image; 246 return image;
247 } 247 }
248 /** 248 /**
249 * Compute the minimum size. 249 * Compute the minimum size.
250 */ 250 */
251 private Point getTotalSize(Image image, char[] text) { 251 private Point getTotalSize(Image image, String text) {
252 Point size = new Point(0, 0); 252 Point size = new Point(0, 0);
253 253
254 if (image !is null) { 254 if (image !is null) {
255 Rectangle r = image.getBounds(); 255 Rectangle r = image.getBounds();
256 size.x += r.width; 256 size.x += r.width;
284 /** 284 /**
285 * Return the Label's text. 285 * Return the Label's text.
286 * 286 *
287 * @return the text of the label or null 287 * @return the text of the label or null
288 */ 288 */
289 public char[] getText() { 289 public String getText() {
290 //checkWidget(); 290 //checkWidget();
291 return text; 291 return text;
292 } 292 }
293 public override char[] getToolTipText () { 293 public override String getToolTipText () {
294 checkWidget(); 294 checkWidget();
295 return appToolTipText; 295 return appToolTipText;
296 } 296 }
297 private void initAccessible() { 297 private void initAccessible() {
298 Accessible accessible = getAccessible(); 298 Accessible accessible = getAccessible();
377 void onPaint(PaintEvent event) { 377 void onPaint(PaintEvent event) {
378 Rectangle rect = getClientArea(); 378 Rectangle rect = getClientArea();
379 if (rect.width is 0 || rect.height is 0) return; 379 if (rect.width is 0 || rect.height is 0) return;
380 380
381 bool shortenText_ = false; 381 bool shortenText_ = false;
382 char[] t = text; 382 String t = text;
383 Image img = image; 383 Image img = image;
384 int availableWidth = Math.max(0, rect.width - 2*hIndent); 384 int availableWidth = Math.max(0, rect.width - 2*hIndent);
385 Point extent = getTotalSize(img, t); 385 Point extent = getTotalSize(img, t);
386 if (extent.x > availableWidth) { 386 if (extent.x > availableWidth) {
387 img = null; 387 img = null;
390 shortenText_ = true; 390 shortenText_ = true;
391 } 391 }
392 } 392 }
393 393
394 GC gc = event.gc; 394 GC gc = event.gc;
395 char[][] lines = text is null ? null : splitString(text); 395 String[] lines = text is null ? null : splitString(text);
396 396
397 // shorten the text 397 // shorten the text
398 if (shortenText_) { 398 if (shortenText_) {
399 extent.x = 0; 399 extent.x = 0;
400 for(int i = 0; i < lines.length; i++) { 400 for(int i = 0; i < lines.length; i++) {
763 * @exception DWTException <ul> 763 * @exception DWTException <ul>
764 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> 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> 765 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
766 * </ul> 766 * </ul>
767 */ 767 */
768 public void setText(char[] text) { 768 public void setText(String text) {
769 checkWidget(); 769 checkWidget();
770 if (text is null) text = ""; //$NON-NLS-1$ 770 if (text is null) text = ""; //$NON-NLS-1$
771 if ( text !=/*eq*/ this.text) { 771 if ( text !=/*eq*/ this.text) {
772 this.text = text; 772 this.text = text;
773 redraw(); 773 redraw();
774 } 774 }
775 } 775 }
776 public override void setToolTipText (char[] string) { 776 public override void setToolTipText (String string) {
777 super.setToolTipText (string); 777 super.setToolTipText (string);
778 appToolTipText = super.getToolTipText(); 778 appToolTipText = super.getToolTipText();
779 } 779 }
780 /** 780 /**
781 * Shorten the given text <code>t</code> so that its length doesn't exceed 781 * Shorten the given text <code>t</code> so that its length doesn't exceed
786 * @param gc the gc to use for text measurement 786 * @param gc the gc to use for text measurement
787 * @param t the text to shorten 787 * @param t the text to shorten
788 * @param width the width to shorten the text to, in pixels 788 * @param width the width to shorten the text to, in pixels
789 * @return the shortened text 789 * @return the shortened text
790 */ 790 */
791 protected char[] shortenText(GC gc, char[] t, int width) { 791 protected String shortenText(GC gc, String t, int width) {
792 if (t is null) return null; 792 if (t is null) return null;
793 int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x; 793 int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x;
794 if (width<=w) return t; 794 if (width<=w) return t;
795 int l = t.length; 795 int l = t.length;
796 int max = l/2; 796 int max = l/2;
797 int min = 0; 797 int min = 0;
798 int mid = (max+min)/2 - 1; 798 int mid = (max+min)/2 - 1;
799 if (mid <= 0) return t; 799 if (mid <= 0) return t;
800 while (min < mid && mid < max) { 800 while (min < mid && mid < max) {
801 char[] s1 = t[0 .. mid].dup; 801 String s1 = t[0 .. mid].dup;
802 char[] s2 = t[l-mid .. l].dup; 802 String s2 = t[l-mid .. l].dup;
803 int l1 = gc.textExtent(s1, DRAW_FLAGS).x; 803 int l1 = gc.textExtent(s1, DRAW_FLAGS).x;
804 int l2 = gc.textExtent(s2, DRAW_FLAGS).x; 804 int l2 = gc.textExtent(s2, DRAW_FLAGS).x;
805 if (l1+w+l2 > width) { 805 if (l1+w+l2 > width) {
806 max = mid; 806 max = mid;
807 mid = (max+min)/2; 807 mid = (max+min)/2;
814 } 814 }
815 if (mid is 0) return t; 815 if (mid is 0) return t;
816 return t[ 0 .. mid ] ~ ELLIPSIS ~ t[ l-mid .. l ]; 816 return t[ 0 .. mid ] ~ ELLIPSIS ~ t[ l-mid .. l ];
817 } 817 }
818 818
819 private char[][] splitString(char[] text) { 819 private String[] splitString(String text) {
820 char[][] lines = new char[][1]; 820 String[] lines = new String[1];
821 int start = 0, pos; 821 int start = 0, pos;
822 do { 822 do {
823 pos = tango.text.Util.locate( text, '\n', start); 823 pos = tango.text.Util.locate( text, '\n', start);
824 if (pos is text.length ) { 824 if (pos is text.length ) {
825 lines[lines.length - 1] = text[start .. $ ]; 825 lines[lines.length - 1] = text[start .. $ ];
826 } else { 826 } else {
827 bool crlf = (pos > 0) && (text[ pos - 1 ] is '\r'); 827 bool crlf = (pos > 0) && (text[ pos - 1 ] is '\r');
828 lines[lines.length - 1] = text[ start .. pos - (crlf ? 1 : 0)]; 828 lines[lines.length - 1] = text[ start .. pos - (crlf ? 1 : 0)];
829 start = pos + 1; 829 start = pos + 1;
830 char[][] newLines = new char[][lines.length+1]; 830 String[] newLines = new String[lines.length+1];
831 System.arraycopy(lines, 0, newLines, 0, lines.length); 831 System.arraycopy(lines, 0, newLines, 0, lines.length);
832 lines = newLines; 832 lines = newLines;
833 } 833 }
834 } while (pos !is text.length); 834 } while (pos !is text.length);
835 return lines; 835 return lines;