comparison dwt/widgets/DateTime.d @ 238:380bad9f6852

reverted char[] to String
author Frank Benoit <benoit@tionex.de>
date Mon, 05 May 2008 00:42:55 +0200
parents 4085a50170ab
children ce446666f5a2
comparison
equal deleted inserted replaced
237:98b80b00af79 238:380bad9f6852
276 } 276 }
277 } 277 }
278 278
279 279
280 private class DateFormatSymbols { 280 private class DateFormatSymbols {
281 private const char[][] ampm = [ "AM"[], "PM" ]; 281 private const String[] ampm = [ "AM"[], "PM" ];
282 char[][] getAmPmStrings(){ 282 String[] getAmPmStrings(){
283 return ampm; 283 return ampm;
284 } 284 }
285 } 285 }
286 286
287 /+ 287 /+
379 /* Emulated DATE and TIME variables */ 379 /* Emulated DATE and TIME variables */
380 Calendar calendar; 380 Calendar calendar;
381 DateFormatSymbols formatSymbols; 381 DateFormatSymbols formatSymbols;
382 Button down, up; 382 Button down, up;
383 Text text; 383 Text text;
384 char[] format; 384 String format;
385 Point[] fieldIndices; 385 Point[] fieldIndices;
386 int[] fieldNames; 386 int[] fieldNames;
387 int fieldCount, currentField = 0, characterCount = 0; 387 int fieldCount, currentField = 0, characterCount = 0;
388 bool ignoreVerify = false; 388 bool ignoreVerify = false;
389 static const char[] DEFAULT_SHORT_DATE_FORMAT = "MM/YYYY"; 389 static const String DEFAULT_SHORT_DATE_FORMAT = "MM/YYYY";
390 static const char[] DEFAULT_MEDIUM_DATE_FORMAT = "MM/DD/YYYY"; 390 static const String DEFAULT_MEDIUM_DATE_FORMAT = "MM/DD/YYYY";
391 static const char[] DEFAULT_LONG_DATE_FORMAT = "MM/DD/YYYY"; 391 static const String DEFAULT_LONG_DATE_FORMAT = "MM/DD/YYYY";
392 static const char[] DEFAULT_SHORT_TIME_FORMAT = "HH:MM AM"; 392 static const String DEFAULT_SHORT_TIME_FORMAT = "HH:MM AM";
393 static const char[] DEFAULT_MEDIUM_TIME_FORMAT = "HH:MM:SS AM"; 393 static const String DEFAULT_MEDIUM_TIME_FORMAT = "HH:MM:SS AM";
394 static const char[] DEFAULT_LONG_TIME_FORMAT = "HH:MM:SS AM"; 394 static const String DEFAULT_LONG_TIME_FORMAT = "HH:MM:SS AM";
395 395
396 396
397 397
398 /** 398 /**
399 * Constructs a new instance of this class given its parent 399 * Constructs a new instance of this class given its parent
589 if (characterCount > 0) { 589 if (characterCount > 0) {
590 characterCount = 0; 590 characterCount = 0;
591 int fieldName = fieldNames[currentField]; 591 int fieldName = fieldNames[currentField];
592 int start = fieldIndices[currentField].x; 592 int start = fieldIndices[currentField].x;
593 int end = fieldIndices[currentField].y; 593 int end = fieldIndices[currentField].y;
594 char[] value = text.getText(start, end - 1); 594 String value = text.getText(start, end - 1);
595 int s = value.lastIndexOf(' '); 595 int s = value.lastIndexOf(' ');
596 if (s !is -1) value = value.substring(s + 1); 596 if (s !is -1) value = value.substring(s + 1);
597 int newValue = unformattedIntValue(fieldName, value, characterCount is 0, calendar.getActualMaximum(fieldName)); 597 int newValue = unformattedIntValue(fieldName, value, characterCount is 0, calendar.getActualMaximum(fieldName));
598 if (newValue !is -1) setTextField(fieldName, newValue, true, true); 598 if (newValue !is -1) setTextField(fieldName, newValue, true, true);
599 } 599 }
600 } 600 }
601 601
602 char[] formattedStringValue(int fieldName, int value, bool adjust) { 602 String formattedStringValue(int fieldName, int value, bool adjust) {
603 if (fieldName is Calendar.AM_PM) { 603 if (fieldName is Calendar.AM_PM) {
604 char[][] ampm = formatSymbols.getAmPmStrings(); 604 String[] ampm = formatSymbols.getAmPmStrings();
605 return ampm[value]; 605 return ampm[value];
606 } 606 }
607 if (adjust) { 607 if (adjust) {
608 if (fieldName is Calendar.HOUR && value is 0) { 608 if (fieldName is Calendar.HOUR && value is 0) {
609 return to!(char[])(12); 609 return to!(String)(12);
610 } 610 }
611 if (fieldName is Calendar.MONTH) { 611 if (fieldName is Calendar.MONTH) {
612 return to!(char[])(value + 1); 612 return to!(String)(value + 1);
613 } 613 }
614 } 614 }
615 return to!(char[])(value); 615 return to!(String)(value);
616 } 616 }
617 617
618 char[] getComputeSizeString(int style) { 618 String getComputeSizeString(int style) {
619 if ((style & DWT.DATE) !is 0) { 619 if ((style & DWT.DATE) !is 0) {
620 return (style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_DATE_FORMAT : (style & DWT.LONG) !is 0 ? DEFAULT_LONG_DATE_FORMAT : DEFAULT_MEDIUM_DATE_FORMAT; 620 return (style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_DATE_FORMAT : (style & DWT.LONG) !is 0 ? DEFAULT_LONG_DATE_FORMAT : DEFAULT_MEDIUM_DATE_FORMAT;
621 } 621 }
622 // DWT.TIME 622 // DWT.TIME
623 return (style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_TIME_FORMAT : (style & DWT.LONG) !is 0 ? DEFAULT_LONG_TIME_FORMAT : DEFAULT_MEDIUM_TIME_FORMAT; 623 return (style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_TIME_FORMAT : (style & DWT.LONG) !is 0 ? DEFAULT_LONG_TIME_FORMAT : DEFAULT_MEDIUM_TIME_FORMAT;
630 } 630 }
631 } 631 }
632 return -1; 632 return -1;
633 } 633 }
634 634
635 char[] getFormattedString(int style) { 635 String getFormattedString(int style) {
636 if ((style & DWT.TIME) !is 0) { 636 if ((style & DWT.TIME) !is 0) {
637 char[][] ampm = formatSymbols.getAmPmStrings(); 637 String[] ampm = formatSymbols.getAmPmStrings();
638 int h = calendar.get(Calendar.HOUR); if (h is 0) h = 12; 638 int h = calendar.get(Calendar.HOUR); if (h is 0) h = 12;
639 int m = calendar.get(Calendar.MINUTE); 639 int m = calendar.get(Calendar.MINUTE);
640 int s = calendar.get(Calendar.SECOND); 640 int s = calendar.get(Calendar.SECOND);
641 int a = calendar.get(Calendar.AM_PM); 641 int a = calendar.get(Calendar.AM_PM);
642 if ((style & DWT.SHORT) !is 0) return "" ~ (h < 10 ? " " : "") ~ to!(char[])(h) ~ ":" ~ (m < 10 ? " " : "") ~ to!(char[])(m) ~ " " ~ ampm[a]; 642 if ((style & DWT.SHORT) !is 0) return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ " " ~ ampm[a];
643 return "" ~ (h < 10 ? " " : "") ~ to!(char[])(h) ~ ":" ~ (m < 10 ? " " : "") ~ to!(char[])(m) ~ ":" ~ (s < 10 ? " " : "") ~ to!(char[])(s) ~ " " ~ ampm[a]; 643 return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ ":" ~ (s < 10 ? " " : "") ~ to!(String)(s) ~ " " ~ ampm[a];
644 } 644 }
645 /* DWT.DATE */ 645 /* DWT.DATE */
646 int y = calendar.get(Calendar.YEAR); 646 int y = calendar.get(Calendar.YEAR);
647 int m = calendar.get(Calendar.MONTH) + 1; 647 int m = calendar.get(Calendar.MONTH) + 1;
648 int d = calendar.get(Calendar.DAY_OF_MONTH); 648 int d = calendar.get(Calendar.DAY_OF_MONTH);
649 if ((style & DWT.SHORT) !is 0) return "" ~ (m < 10 ? " " : "") ~ to!(char[])(m) ~ "/" ~ to!(char[])(y); 649 if ((style & DWT.SHORT) !is 0) return "" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ "/" ~ to!(String)(y);
650 return "" ~ (m < 10 ? " " : "") ~ to!(char[])(m) ~ "/" ~ (d < 10 ? " " : "") ~ to!(char[])(d) ~ "/" ~ to!(char[])(y); 650 return "" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ "/" ~ (d < 10 ? " " : "") ~ to!(String)(d) ~ "/" ~ to!(String)(y);
651 } 651 }
652 652
653 void getDate() { 653 void getDate() {
654 uint y; 654 uint y;
655 uint m; 655 uint m;
928 event.doit = false; 928 event.doit = false;
929 int fieldName = fieldNames[currentField]; 929 int fieldName = fieldNames[currentField];
930 int start = fieldIndices[currentField].x; 930 int start = fieldIndices[currentField].x;
931 int end = fieldIndices[currentField].y; 931 int end = fieldIndices[currentField].y;
932 int length_ = end - start; 932 int length_ = end - start;
933 char[] newText = event.text; 933 String newText = event.text;
934 if (fieldName is Calendar.AM_PM) { 934 if (fieldName is Calendar.AM_PM) {
935 char[][] ampm = formatSymbols.getAmPmStrings(); 935 String[] ampm = formatSymbols.getAmPmStrings();
936 if (newText.equalsIgnoreCase(ampm[Calendar.AM].substring(0, 1)) || newText.equalsIgnoreCase(ampm[Calendar.AM])) { 936 if (newText.equalsIgnoreCase(ampm[Calendar.AM].substring(0, 1)) || newText.equalsIgnoreCase(ampm[Calendar.AM])) {
937 setTextField(fieldName, Calendar.AM, true, false); 937 setTextField(fieldName, Calendar.AM, true, false);
938 } else if (newText.equalsIgnoreCase(ampm[Calendar.PM].substring(0, 1)) || newText.equalsIgnoreCase(ampm[Calendar.PM])) { 938 } else if (newText.equalsIgnoreCase(ampm[Calendar.PM].substring(0, 1)) || newText.equalsIgnoreCase(ampm[Calendar.PM])) {
939 setTextField(fieldName, Calendar.PM, true, false); 939 setTextField(fieldName, Calendar.PM, true, false);
940 } 940 }
944 try { 944 try {
945 to!(int)(newText); 945 to!(int)(newText);
946 } catch (ConversionException ex) { 946 } catch (ConversionException ex) {
947 return; 947 return;
948 } 948 }
949 char[] value = text.getText(start, end - 1); 949 String value = text.getText(start, end - 1);
950 int s = value.lastIndexOf(' '); 950 int s = value.lastIndexOf(' ');
951 if (s !is -1) value = value.substring(s + 1); 951 if (s !is -1) value = value.substring(s + 1);
952 newText = value ~ newText; 952 newText = value ~ newText;
953 } 953 }
954 int newTextLength = newText.length; 954 int newTextLength = newText.length;
1023 this( int start, int end ){ 1023 this( int start, int end ){
1024 this.start = start; this.end = end; 1024 this.start = start; this.end = end;
1025 } 1025 }
1026 public void run() { 1026 public void run() {
1027 if (!text.isDisposed()) { 1027 if (!text.isDisposed()) {
1028 char[] value = text.getText(start, end - 1); 1028 String value = text.getText(start, end - 1);
1029 int s = value.lastIndexOf(' '); 1029 int s = value.lastIndexOf(' ');
1030 if (s is -1 ) s = start; 1030 if (s is -1 ) s = start;
1031 else s = start + s + 1; 1031 else s = start + s + 1;
1032 text.setSelection(s, end); 1032 text.setSelection(s, end);
1033 } 1033 }
1068 checkWidget(); 1068 checkWidget();
1069 super.setForeground(color); 1069 super.setForeground(color);
1070 if (text !is null) text.setForeground(color); 1070 if (text !is null) text.setForeground(color);
1071 } 1071 }
1072 1072
1073 /*public*/ void setFormat(char[] string) { 1073 /*public*/ void setFormat(String string) {
1074 checkWidget(); 1074 checkWidget();
1075 // TODO: this needs to be locale sensitive 1075 // TODO: this needs to be locale sensitive
1076 fieldCount = (style & DWT.DATE) !is 0 ? ((style & DWT.SHORT) !is 0 ? 2 : 3) : ((style & DWT.SHORT) !is 0 ? 3 : 4); 1076 fieldCount = (style & DWT.DATE) !is 0 ? ((style & DWT.SHORT) !is 0 ? 2 : 3) : ((style & DWT.SHORT) !is 0 ? 3 : 4);
1077 fieldIndices = new Point[fieldCount]; 1077 fieldIndices = new Point[fieldCount];
1078 fieldNames = new int[fieldCount]; 1078 fieldNames = new int[fieldCount];
1131 if (value < min) value = max; // wrap 1131 if (value < min) value = max; // wrap
1132 } 1132 }
1133 int start = fieldIndices[currentField].x; 1133 int start = fieldIndices[currentField].x;
1134 int end = fieldIndices[currentField].y; 1134 int end = fieldIndices[currentField].y;
1135 text.setSelection(start, end); 1135 text.setSelection(start, end);
1136 char[] newValue = formattedStringValue(fieldName, value, adjust); 1136 String newValue = formattedStringValue(fieldName, value, adjust);
1137 TangoText buffer = new TangoText(newValue); 1137 TangoText buffer = new TangoText(newValue);
1138 /* Convert leading 0's into spaces. */ 1138 /* Convert leading 0's into spaces. */
1139 int prependCount = end - start - buffer.length(); 1139 int prependCount = end - start - buffer.length();
1140 for (int i = 0; i < prependCount; i++) { 1140 for (int i = 0; i < prependCount; i++) {
1141 buffer.prepend(' '); 1141 buffer.prepend(' ');
1294 calendar.set(Calendar.YEAR, year); 1294 calendar.set(Calendar.YEAR, year);
1295 updateControl(); 1295 updateControl();
1296 } 1296 }
1297 } 1297 }
1298 1298
1299 int unformattedIntValue(int fieldName, char[] newText, bool adjust, int max) { 1299 int unformattedIntValue(int fieldName, String newText, bool adjust, int max) {
1300 int newValue; 1300 int newValue;
1301 try { 1301 try {
1302 newValue = to!(int)(newText); 1302 newValue = to!(int)(newText);
1303 } catch (ConversionException ex) { 1303 } catch (ConversionException ex) {
1304 return -1; 1304 return -1;
1313 return newValue; 1313 return newValue;
1314 } 1314 }
1315 1315
1316 public void updateControl() { 1316 public void updateControl() {
1317 if (text !is null) { 1317 if (text !is null) {
1318 char[] string = getFormattedString(style); 1318 String string = getFormattedString(style);
1319 ignoreVerify = true; 1319 ignoreVerify = true;
1320 text.setText(string); 1320 text.setText(string);
1321 ignoreVerify = false; 1321 ignoreVerify = false;
1322 } 1322 }
1323 redraw(); 1323 redraw();