Author: pulkomandy Date: 2010-07-08 11:14:09 +0200 (Thu, 08 Jul 2010) New Revision: 37427 Changeset: http://dev.haiku-os.org/changeset/37427/haiku Modified: haiku/trunk/headers/os/locale/Country.h haiku/trunk/src/kits/locale/Country.cpp haiku/trunk/src/kits/locale/Locale.cpp haiku/trunk/src/preferences/locale/TimeFormatSettingsView.cpp haiku/trunk/src/preferences/locale/TimeFormatSettingsView.h Log: Support for setting 12/24Hr mode in locale preflet. Modified: haiku/trunk/headers/os/locale/Country.h =================================================================== --- haiku/trunk/headers/os/locale/Country.h 2010-07-08 07:11:55 UTC (rev 37426) +++ haiku/trunk/headers/os/locale/Country.h 2010-07-08 09:14:09 UTC (rev 37427) @@ -45,6 +45,8 @@ bool DateFormat(BString&, bool longFormat) const; void SetDateFormat(const char* formatString, bool longFormat = true); + void SetTimeFormat(const char* formatString, + bool longFormat = true); bool TimeFormat(BString&, bool longFormat) const; const char* DateSeparator() const; const char* TimeSeparator() const; @@ -85,6 +87,8 @@ private: icu_4_2::DateFormat* fICULongDateFormatter; icu_4_2::DateFormat* fICUShortDateFormatter; + icu_4_2::DateFormat* fICULongTimeFormatter; + icu_4_2::DateFormat* fICUShortTimeFormatter; const char** fStrings; icu_4_2::Locale* fICULocale; }; Modified: haiku/trunk/src/kits/locale/Country.cpp =================================================================== --- haiku/trunk/src/kits/locale/Country.cpp 2010-07-08 07:11:55 UTC (rev 37426) +++ haiku/trunk/src/kits/locale/Country.cpp 2010-07-08 09:14:09 UTC (rev 37427) @@ -54,6 +54,14 @@ fStrings(gStrings) { fICULocale = new icu_4_2::Locale(languageCode, countryCode); + fICULongDateFormatter = DateFormat::createDateInstance( + DateFormat::FULL, *fICULocale); + fICUShortDateFormatter = DateFormat::createDateInstance( + DateFormat::SHORT, *fICULocale); + fICULongTimeFormatter = DateFormat::createTimeInstance( + DateFormat::MEDIUM, *fICULocale); + fICUShortTimeFormatter = DateFormat::createTimeInstance( + DateFormat::SHORT, *fICULocale); } @@ -66,11 +74,19 @@ DateFormat::FULL, *fICULocale); fICUShortDateFormatter = DateFormat::createDateInstance( DateFormat::SHORT, *fICULocale); + fICULongTimeFormatter = DateFormat::createTimeInstance( + DateFormat::MEDIUM, *fICULocale); + fICUShortTimeFormatter = DateFormat::createTimeInstance( + DateFormat::SHORT, *fICULocale); } BCountry::~BCountry() { + delete fICULongTimeFormatter; + delete fICUShortTimeFormatter; + delete fICULongDateFormatter; + delete fICUShortDateFormatter; delete fICULocale; } @@ -168,9 +184,7 @@ // TODO: ICU allows for 4 different levels of expansion : // short, medium, long, and full. Our bool parameter is not enough... icu_4_2::DateFormat* timeFormatter; - timeFormatter = DateFormat::createTimeInstance( - longFormat ? DateFormat::MEDIUM : DateFormat::SHORT, - *fICULocale); + timeFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter; UnicodeString ICUString; ICUString = timeFormatter->format((UDate)time * 1000, ICUString); @@ -178,8 +192,6 @@ BStringByteSink stringConverter(string); ICUString.toUTF8(stringConverter); - - delete timeFormatter; } @@ -215,13 +227,24 @@ } +void +BCountry::SetTimeFormat(const char* formatString, bool longFormat) +{ + icu_4_2::DateFormat* dateFormatter + = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter; + SimpleDateFormat* dateFormatterImpl + = static_cast<SimpleDateFormat*>(dateFormatter); + + UnicodeString pattern(formatString); + dateFormatterImpl->applyPattern(pattern); +} + + bool BCountry::TimeFormat(BString& format, bool longFormat) const { icu_4_2::DateFormat* dateFormatter; - dateFormatter = DateFormat::createTimeInstance( - longFormat ? DateFormat::FULL : DateFormat::SHORT, - *fICULocale); + dateFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter; SimpleDateFormat* dateFormatterImpl = static_cast<SimpleDateFormat*>(dateFormatter); Modified: haiku/trunk/src/kits/locale/Locale.cpp =================================================================== --- haiku/trunk/src/kits/locale/Locale.cpp 2010-07-08 07:11:55 UTC (rev 37426) +++ haiku/trunk/src/kits/locale/Locale.cpp 2010-07-08 09:14:09 UTC (rev 37427) @@ -38,8 +38,5 @@ return ""; } - if (id >= B_LANGUAGE_STRINGS_BASE) - return fLanguage->GetString(id); - - return fCountry->GetString(id); + return fLanguage->GetString(id); } Modified: haiku/trunk/src/preferences/locale/TimeFormatSettingsView.cpp =================================================================== --- haiku/trunk/src/preferences/locale/TimeFormatSettingsView.cpp 2010-07-08 07:11:55 UTC (rev 37426) +++ haiku/trunk/src/preferences/locale/TimeFormatSettingsView.cpp 2010-07-08 09:14:09 UTC (rev 37427) @@ -6,6 +6,8 @@ #include "TimeFormatSettingsView.h" +#include <iostream> + #include <Alert.h> #include <Catalog.h> #include <CheckBox.h> @@ -139,7 +141,6 @@ return false; } - // #pragma mark - @@ -182,11 +183,18 @@ clockBox->SetLabel(B_TRANSLATE("Clock")); f24HrRadioButton = new BRadioButton("", B_TRANSLATE("24 hour"), - new BMessage(kSettingsContentsModified)); + new BMessage(kClockFormatChange)); f12HrRadioButton = new BRadioButton("", B_TRANSLATE("12 hour"), - new BMessage(kSettingsContentsModified)); + new BMessage(kClockFormatChange)); + BString timeFormat; + fCountry->TimeFormat(timeFormat, false); + if (timeFormat.FindFirst(" a")) + f12HrRadioButton->SetValue(1); + else + f24HrRadioButton->SetValue(1); + float spacing = be_control_look->DefaultItemSpacing(); clockBox->AddChild(BGroupLayoutBuilder(B_VERTICAL) @@ -414,7 +422,8 @@ if (item) { separator = fSeparatorMenuField->Menu()->IndexOf(item); if (separator >= 0) - //settings.SetTimeFormatSeparator((FormatSeparator)separator); + //settings.SetTimeFormatSeparator( + // (FormatSeparator)separator); ; } @@ -427,7 +436,8 @@ notificationMessage.AddInt32("TimeFormatSeparator", separator); notificationMessage.AddBool("24HrClock", f24HrRadioButton->Value() == 1); - // tracker->SendNotices(kDateFormatChanged, ¬ificationMessage); + // tracker->SendNotices(kDateFormatChanged, + // ¬ificationMessage); _UpdateExamples(); @@ -435,6 +445,41 @@ break; } + case kClockFormatChange: + { + BString timeFormat; + fCountry->TimeFormat(timeFormat, false); + if (f24HrRadioButton->Value() == 1) { + timeFormat.ReplaceAll("k","h"); + timeFormat.ReplaceAll("K","H"); + timeFormat.RemoveAll(" a"); + } else { + timeFormat.ReplaceAll("h","k"); + timeFormat.ReplaceAll("H","K"); + timeFormat.Append(" a"); + f12HrRadioButton->SetValue(true); + } + std::cout << timeFormat.String() << std::endl; + fCountry->SetTimeFormat(timeFormat.String(), false); + + timeFormat.Truncate(0); + + fCountry->TimeFormat(timeFormat, true); + if (f24HrRadioButton->Value() == 1) { + timeFormat.ReplaceAll("k","h"); + timeFormat.ReplaceAll("K","H"); + timeFormat.RemoveAll(" a"); + } else { + timeFormat.ReplaceAll("h","k"); + timeFormat.ReplaceAll("H","K"); + timeFormat.Append(" a"); + } + fCountry->SetTimeFormat(timeFormat.String(), true); + _UpdateExamples(); + Window()->PostMessage(kSettingsContentsModified); + break; + } + default: BView::MessageReceived(message); } Modified: haiku/trunk/src/preferences/locale/TimeFormatSettingsView.h =================================================================== --- haiku/trunk/src/preferences/locale/TimeFormatSettingsView.h 2010-07-08 07:11:55 UTC (rev 37426) +++ haiku/trunk/src/preferences/locale/TimeFormatSettingsView.h 2010-07-08 09:14:09 UTC (rev 37427) @@ -30,6 +30,7 @@ }; const uint32 kSettingsContentsModified = 'Scmo'; +const uint32 kClockFormatChange = 'cfmc'; const uint32 kMenuMessage = 'FRMT';