Mercurial > projects > dwt-linux
comparison dwt/widgets/DateTime.d @ 240:ce446666f5a2
Update to SWT 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 12 May 2008 19:13:01 +0200 |
parents | 380bad9f6852 |
children | c0d810de7093 |
comparison
equal
deleted
inserted
replaced
239:06a1f6829310 | 240:ce446666f5a2 |
---|---|
188 case AM_PM: | 188 case AM_PM: |
189 return ( hour < 12 ) ? AM : PM; | 189 return ( hour < 12 ) ? AM : PM; |
190 default: assert( false, Format( "no matching switch case for field {}.", field )); | 190 default: assert( false, Format( "no matching switch case for field {}.", field )); |
191 } | 191 } |
192 } | 192 } |
193 | 193 void set( int year, int month, int day ){ |
194 this.year = year; | |
195 this.month = month; | |
196 this.dayofmonth = day; | |
197 } | |
194 void set(int field, int value){ | 198 void set(int field, int value){ |
195 switch( field ){ | 199 switch( field ){ |
196 case YEAR: | 200 case YEAR: |
197 year = value; | 201 year = value; |
198 break; | 202 break; |
431 /* DWT.DATE and DWT.TIME */ | 435 /* DWT.DATE and DWT.TIME */ |
432 calendar = Calendar.getInstance(); | 436 calendar = Calendar.getInstance(); |
433 formatSymbols = new DateFormatSymbols(); | 437 formatSymbols = new DateFormatSymbols(); |
434 | 438 |
435 text = new Text(this, DWT.SINGLE); | 439 text = new Text(this, DWT.SINGLE); |
440 /* disable the native drag and drop for the date/time text field */ | |
441 OS.gtk_drag_dest_unset(text.handle); | |
436 if ((this.style & DWT.DATE) !is 0) { | 442 if ((this.style & DWT.DATE) !is 0) { |
437 setFormat((this.style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_DATE_FORMAT : (this.style & DWT.LONG) !is 0 ? DEFAULT_LONG_DATE_FORMAT : DEFAULT_MEDIUM_DATE_FORMAT); | 443 setFormat((this.style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_DATE_FORMAT : (this.style & DWT.LONG) !is 0 ? DEFAULT_LONG_DATE_FORMAT : DEFAULT_MEDIUM_DATE_FORMAT); |
438 } else { // DWT.TIME | 444 } else { // DWT.TIME |
439 setFormat((this.style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_TIME_FORMAT : (this.style & DWT.LONG) !is 0 ? DEFAULT_LONG_TIME_FORMAT : DEFAULT_MEDIUM_TIME_FORMAT); | 445 setFormat((this.style & DWT.SHORT) !is 0 ? DEFAULT_SHORT_TIME_FORMAT : (this.style & DWT.LONG) !is 0 ? DEFAULT_LONG_TIME_FORMAT : DEFAULT_MEDIUM_TIME_FORMAT); |
440 } | 446 } |
637 String[] ampm = formatSymbols.getAmPmStrings(); | 643 String[] ampm = formatSymbols.getAmPmStrings(); |
638 int h = calendar.get(Calendar.HOUR); if (h is 0) h = 12; | 644 int h = calendar.get(Calendar.HOUR); if (h is 0) h = 12; |
639 int m = calendar.get(Calendar.MINUTE); | 645 int m = calendar.get(Calendar.MINUTE); |
640 int s = calendar.get(Calendar.SECOND); | 646 int s = calendar.get(Calendar.SECOND); |
641 int a = calendar.get(Calendar.AM_PM); | 647 int a = calendar.get(Calendar.AM_PM); |
642 if ((style & DWT.SHORT) !is 0) return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ " " ~ ampm[a]; | 648 if ((style & DWT.SHORT) !is 0) return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? "0" : "") ~ to!(String)(m) ~ " " ~ ampm[a]; |
643 return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ ":" ~ (s < 10 ? " " : "") ~ to!(String)(s) ~ " " ~ ampm[a]; | 649 return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? "0" : "") ~ to!(String)(m) ~ ":" ~ (s < 10 ? "0" : "") ~ to!(String)(s) ~ " " ~ ampm[a]; |
644 } | 650 } |
645 /* DWT.DATE */ | 651 /* DWT.DATE */ |
646 int y = calendar.get(Calendar.YEAR); | 652 int y = calendar.get(Calendar.YEAR); |
647 int m = calendar.get(Calendar.MONTH) + 1; | 653 int m = calendar.get(Calendar.MONTH) + 1; |
648 int d = calendar.get(Calendar.DAY_OF_MONTH); | 654 int d = calendar.get(Calendar.DAY_OF_MONTH); |
748 } else { | 754 } else { |
749 return calendar.get(Calendar.MONTH); | 755 return calendar.get(Calendar.MONTH); |
750 } | 756 } |
751 } | 757 } |
752 | 758 |
759 String getNameText() { | |
760 if((style & DWT.TIME) !is 0){ | |
761 return Format( "{}:{}:{}", getHours(), getMinutes(), getSeconds() ); | |
762 } | |
763 else{ | |
764 return Format( "{}/{}/{}", (getMonth() + 1), getDay(), getYear() ); | |
765 } | |
766 } | |
767 | |
753 /** | 768 /** |
754 * Returns the receiver's seconds. | 769 * Returns the receiver's seconds. |
755 * <p> | 770 * <p> |
756 * Seconds is an integer between 0 and 59. | 771 * Seconds is an integer between 0 and 59. |
757 * </p> | 772 * </p> |
823 validCalendar = calendar; | 838 validCalendar = calendar; |
824 } | 839 } |
825 int min = validCalendar.getActualMinimum(fieldName); | 840 int min = validCalendar.getActualMinimum(fieldName); |
826 int max = validCalendar.getActualMaximum(fieldName); | 841 int max = validCalendar.getActualMaximum(fieldName); |
827 return value >= min && value <= max; | 842 return value >= min && value <= max; |
843 } | |
844 | |
845 bool isValid(int year, int month, int day) { | |
846 Calendar valid = Calendar.getInstance(); | |
847 valid.set(year, month, day); | |
848 return valid.get(Calendar.YEAR) is year && valid.get(Calendar.MONTH) is month && valid.get(Calendar.DAY_OF_MONTH) is day; | |
828 } | 849 } |
829 | 850 |
830 void incrementField(int amount) { | 851 void incrementField(int amount) { |
831 int fieldName = fieldNames[currentField]; | 852 int fieldName = fieldNames[currentField]; |
832 int value = calendar.get(fieldName); | 853 int value = calendar.get(fieldName); |
1109 if (calendar.get(fieldName) is value) return; | 1130 if (calendar.get(fieldName) is value) return; |
1110 if (fieldName is Calendar.AM_PM) { | 1131 if (fieldName is Calendar.AM_PM) { |
1111 calendar.roll(Calendar.HOUR_OF_DAY, 12); // TODO: needs more work for setFormat and locale | 1132 calendar.roll(Calendar.HOUR_OF_DAY, 12); // TODO: needs more work for setFormat and locale |
1112 } | 1133 } |
1113 calendar.set(fieldName, value); | 1134 calendar.set(fieldName, value); |
1114 notifyListeners(DWT.Selection, new Event()); | 1135 postEvent(DWT.Selection); |
1115 } | 1136 } |
1116 | 1137 |
1117 void setTextField(int fieldName, int value, bool commit, bool adjust) { | 1138 void setTextField(int fieldName, int value, bool commit, bool adjust) { |
1118 if (commit) { | 1139 if (commit) { |
1119 int max = calendar.getActualMaximum(fieldName); | 1140 int max = calendar.getActualMaximum(fieldName); |
1136 String newValue = formattedStringValue(fieldName, value, adjust); | 1157 String newValue = formattedStringValue(fieldName, value, adjust); |
1137 TangoText buffer = new TangoText(newValue); | 1158 TangoText buffer = new TangoText(newValue); |
1138 /* Convert leading 0's into spaces. */ | 1159 /* Convert leading 0's into spaces. */ |
1139 int prependCount = end - start - buffer.length(); | 1160 int prependCount = end - start - buffer.length(); |
1140 for (int i = 0; i < prependCount; i++) { | 1161 for (int i = 0; i < prependCount; i++) { |
1141 buffer.prepend(' '); | 1162 switch (fieldName) { |
1163 case Calendar.MINUTE: | |
1164 case Calendar.SECOND: | |
1165 buffer.prepend('0'); | |
1166 break; | |
1167 default: | |
1168 buffer.prepend(' '); | |
1169 break; | |
1170 } | |
1142 } | 1171 } |
1143 newValue = buffer.toString(); | 1172 newValue = buffer.toString(); |
1144 ignoreVerify = true; | 1173 ignoreVerify = true; |
1145 text.insert(newValue); | 1174 text.insert(newValue); |
1146 ignoreVerify = false; | 1175 ignoreVerify = false; |
1147 selectField(currentField); | 1176 selectField(currentField); |
1148 if (commit) setField(fieldName, value); | 1177 if (commit) setField(fieldName, value); |
1149 } | 1178 } |
1150 | 1179 |
1151 /** | 1180 /** |
1181 * Sets the receiver's year, month, and day in a single operation. | |
1182 * <p> | |
1183 * This is the recommended way to set the date, because setting the year, | |
1184 * month, and day separately may result in invalid intermediate dates. | |
1185 * </p> | |
1186 * | |
1187 * @param year an integer between 1752 and 9999 | |
1188 * @param month an integer between 0 and 11 | |
1189 * @param day a positive integer beginning with 1 | |
1190 * | |
1191 * @exception DWTException <ul> | |
1192 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1193 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1194 * </ul> | |
1195 * | |
1196 * @since 3.4 | |
1197 */ | |
1198 public void setDate (int year, int month, int day) { | |
1199 checkWidget (); | |
1200 if (!isValid(year, month, day)) return; | |
1201 if ((style & DWT.CALENDAR) !is 0) { | |
1202 this.year = year; | |
1203 this.month = month; | |
1204 this.day = day; | |
1205 OS.gtk_calendar_select_month(handle, month, year); | |
1206 OS.gtk_calendar_select_day(handle, day); | |
1207 } else { | |
1208 calendar.set(Calendar.YEAR, year); | |
1209 calendar.set(Calendar.MONTH, month); | |
1210 calendar.set(Calendar.DAY_OF_MONTH, day); | |
1211 updateControl(); | |
1212 } | |
1213 } | |
1214 | |
1215 /** | |
1152 * Sets the receiver's date, or day of the month, to the specified day. | 1216 * Sets the receiver's date, or day of the month, to the specified day. |
1153 * <p> | 1217 * <p> |
1154 * The first day of the month is 1, and the last day depends on the month and year. | 1218 * The first day of the month is 1, and the last day depends on the month and year. |
1155 * </p> | 1219 * </p> |
1156 * | 1220 * |
1263 checkWidget (); | 1327 checkWidget (); |
1264 if (!isValid(Calendar.SECOND, seconds)) return; | 1328 if (!isValid(Calendar.SECOND, seconds)) return; |
1265 if ((style & DWT.CALENDAR) !is 0) { | 1329 if ((style & DWT.CALENDAR) !is 0) { |
1266 this.seconds = seconds; | 1330 this.seconds = seconds; |
1267 } else { | 1331 } else { |
1332 calendar.set(Calendar.SECOND, seconds); | |
1333 updateControl(); | |
1334 } | |
1335 } | |
1336 | |
1337 /** | |
1338 * Sets the receiver's hours, minutes, and seconds in a single operation. | |
1339 * | |
1340 * @param hours an integer between 0 and 23 | |
1341 * @param minutes an integer between 0 and 59 | |
1342 * @param seconds an integer between 0 and 59 | |
1343 * | |
1344 * @exception DWTException <ul> | |
1345 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
1346 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
1347 * </ul> | |
1348 * | |
1349 * @since 3.4 | |
1350 */ | |
1351 public void setTime (int hours, int minutes, int seconds) { | |
1352 checkWidget (); | |
1353 if (!isValid(Calendar.HOUR_OF_DAY, hours)) return; | |
1354 if (!isValid(Calendar.MINUTE, minutes)) return; | |
1355 if (!isValid(Calendar.SECOND, seconds)) return; | |
1356 if ((style & DWT.CALENDAR) !is 0) { | |
1357 this.hours = hours; | |
1358 this.minutes = minutes; | |
1359 this.seconds = seconds; | |
1360 } else { | |
1361 calendar.set(Calendar.HOUR_OF_DAY, hours); | |
1362 calendar.set(Calendar.MINUTE, minutes); | |
1268 calendar.set(Calendar.SECOND, seconds); | 1363 calendar.set(Calendar.SECOND, seconds); |
1269 updateControl(); | 1364 updateControl(); |
1270 } | 1365 } |
1271 } | 1366 } |
1272 | 1367 |