Support for setting 12/24Hr mode in locale preflet.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37427 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Adrien Destugues 2010-07-08 09:14:09 +00:00
parent 4b0427bddc
commit 51145d87bd
5 changed files with 87 additions and 17 deletions

View File

@ -45,6 +45,8 @@ class BCountry {
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 @@ class BCountry {
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;
};

View File

@ -54,6 +54,14 @@ BCountry::BCountry(const char* languageCode, const char* countryCode)
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 @@ BCountry::BCountry(const char* languageAndCountryCode)
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 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat)
// 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 @@ BCountry::FormatTime(BString* string, time_t time, bool longFormat)
BStringByteSink stringConverter(string);
ICUString.toUTF8(stringConverter);
delete timeFormatter;
}
@ -215,13 +227,24 @@ BCountry::SetDateFormat(const char* formatString, bool longFormat)
}
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);

View File

@ -38,8 +38,5 @@ BLocale::GetString(uint32 id)
return "";
}
if (id >= B_LANGUAGE_STRINGS_BASE)
return fLanguage->GetString(id);
return fCountry->GetString(id);
return fLanguage->GetString(id);
}

View File

@ -6,6 +6,8 @@
#include "TimeFormatSettingsView.h"
#include <iostream>
#include <Alert.h>
#include <Catalog.h>
#include <CheckBox.h>
@ -139,7 +141,6 @@ IsSpecialDateChar(char charToTest)
return false;
}
// #pragma mark -
@ -182,10 +183,17 @@ FormatView::FormatView(BCountry* country)
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();
@ -414,7 +422,8 @@ FormatView::MessageReceived(BMessage* message)
if (item) {
separator = fSeparatorMenuField->Menu()->IndexOf(item);
if (separator >= 0)
//settings.SetTimeFormatSeparator((FormatSeparator)separator);
//settings.SetTimeFormatSeparator(
// (FormatSeparator)separator);
;
}
@ -427,7 +436,8 @@ FormatView::MessageReceived(BMessage* message)
notificationMessage.AddInt32("TimeFormatSeparator", separator);
notificationMessage.AddBool("24HrClock",
f24HrRadioButton->Value() == 1);
// tracker->SendNotices(kDateFormatChanged, &notificationMessage);
// tracker->SendNotices(kDateFormatChanged,
// &notificationMessage);
_UpdateExamples();
@ -435,6 +445,41 @@ FormatView::MessageReceived(BMessage* message)
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);
}

View File

@ -30,6 +30,7 @@ enum FormatSeparator {
};
const uint32 kSettingsContentsModified = 'Scmo';
const uint32 kClockFormatChange = 'cfmc';
const uint32 kMenuMessage = 'FRMT';