2 * Copyright 2009, Adrien Destugues, pulkomandy@gmail.com. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include "FormatSettingsView.h"
12 #include <Application.h>
15 #include <ControlLook.h>
16 #include <DateFormat.h>
17 #include <GridLayout.h>
18 #include <GridLayoutBuilder.h>
19 #include <GroupLayout.h>
20 #include <GroupLayoutBuilder.h>
21 #include <LayoutBuilder.h>
23 #include <MutableLocaleRoster.h>
26 #include <MenuField.h>
28 #include <NumberFormat.h>
29 #include <PopUpMenu.h>
30 #include <RadioButton.h>
31 #include <ScrollView.h>
32 #include <ScrollBar.h>
33 #include <SeparatorView.h>
34 #include <SpaceLayoutItem.h>
36 #include <StringView.h>
37 #include <TextControl.h>
38 #include <TimeFormat.h>
41 #include "LocalePreflet.h"
44 using BPrivate::MutableLocaleRoster
;
47 #undef B_TRANSLATION_CONTEXT
48 #define B_TRANSLATION_CONTEXT "TimeFormatSettings"
54 FormatSettingsView::FormatSettingsView()
56 BView("WindowsSettingsView", B_FRAME_EVENTS
)
58 fUseLanguageStringsCheckBox
= new BCheckBox(
59 B_TRANSLATE("Use month/day-names from preferred language"),
60 new BMessage(kStringsLanguageChange
));
62 BStringView
* fullDateLabel
63 = new BStringView("", B_TRANSLATE("Full format:"));
64 fullDateLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
65 B_ALIGN_VERTICAL_UNSET
));
66 BStringView
* longDateLabel
67 = new BStringView("", B_TRANSLATE("Long format:"));
68 longDateLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
69 B_ALIGN_VERTICAL_UNSET
));
70 BStringView
* mediumDateLabel
71 = new BStringView("", B_TRANSLATE("Medium format:"));
72 mediumDateLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
73 B_ALIGN_VERTICAL_UNSET
));
74 BStringView
* shortDateLabel
75 = new BStringView("", B_TRANSLATE("Short format:"));
76 shortDateLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
77 B_ALIGN_VERTICAL_UNSET
));
79 fFullDateExampleView
= new BStringView("", "");
80 fFullDateExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
81 B_ALIGN_VERTICAL_UNSET
));
82 fLongDateExampleView
= new BStringView("", "");
83 fLongDateExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
84 B_ALIGN_VERTICAL_UNSET
));
85 fMediumDateExampleView
= new BStringView("", "");
86 fMediumDateExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
87 B_ALIGN_VERTICAL_UNSET
));
88 fShortDateExampleView
= new BStringView("", "");
89 fShortDateExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
90 B_ALIGN_VERTICAL_UNSET
));
92 f24HourRadioButton
= new BRadioButton("", B_TRANSLATE("24 hour"),
93 new BMessage(kClockFormatChange
));
94 f12HourRadioButton
= new BRadioButton("", B_TRANSLATE("12 hour"),
95 new BMessage(kClockFormatChange
));
97 float spacing
= be_control_look
->DefaultItemSpacing();
99 BStringView
* fullTimeLabel
100 = new BStringView("", B_TRANSLATE("Full format:"));
101 fullTimeLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
102 B_ALIGN_VERTICAL_UNSET
));
103 BStringView
* longTimeLabel
104 = new BStringView("", B_TRANSLATE("Long format:"));
105 longTimeLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
106 B_ALIGN_VERTICAL_UNSET
));
107 BStringView
* mediumTimeLabel
108 = new BStringView("", B_TRANSLATE("Medium format:"));
109 mediumTimeLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
110 B_ALIGN_VERTICAL_UNSET
));
111 BStringView
* shortTimeLabel
112 = new BStringView("", B_TRANSLATE("Short format:"));
113 shortTimeLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
114 B_ALIGN_VERTICAL_UNSET
));
116 fFullTimeExampleView
= new BStringView("", "");
117 fFullTimeExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
118 B_ALIGN_VERTICAL_UNSET
));
119 fLongTimeExampleView
= new BStringView("", "");
120 fLongTimeExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
121 B_ALIGN_VERTICAL_UNSET
));
122 fMediumTimeExampleView
= new BStringView("", "");
123 fMediumTimeExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
124 B_ALIGN_VERTICAL_UNSET
));
125 fShortTimeExampleView
= new BStringView("", "");
126 fShortTimeExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
127 B_ALIGN_VERTICAL_UNSET
));
129 BStringView
* positiveNumberLabel
130 = new BStringView("", B_TRANSLATE("Positive:"));
131 positiveNumberLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
132 B_ALIGN_VERTICAL_UNSET
));
133 BStringView
* negativeNumberLabel
134 = new BStringView("", B_TRANSLATE("Negative:"));
135 negativeNumberLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
136 B_ALIGN_VERTICAL_UNSET
));
138 fPositiveNumberExampleView
= new BStringView("", "");
139 fPositiveNumberExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT
,
140 B_ALIGN_VERTICAL_UNSET
));
141 fNegativeNumberExampleView
= new BStringView("", "");
142 fNegativeNumberExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT
,
143 B_ALIGN_VERTICAL_UNSET
));
145 BStringView
* positiveMonetaryLabel
146 = new BStringView("", B_TRANSLATE("Positive:"));
147 positiveMonetaryLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
148 B_ALIGN_VERTICAL_UNSET
));
149 BStringView
* negativeMonetaryLabel
150 = new BStringView("", B_TRANSLATE("Negative:"));
151 negativeMonetaryLabel
->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT
,
152 B_ALIGN_VERTICAL_UNSET
));
154 fPositiveMonetaryExampleView
= new BStringView("", "");
155 fPositiveMonetaryExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT
,
156 B_ALIGN_VERTICAL_UNSET
));
157 fNegativeMonetaryExampleView
= new BStringView("", "");
158 fNegativeMonetaryExampleView
->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT
,
159 B_ALIGN_VERTICAL_UNSET
));
163 fDateBox
= new BBox(B_TRANSLATE("Date"));
164 fTimeBox
= new BBox(B_TRANSLATE("Time"));
165 fNumberBox
= new BBox(B_TRANSLATE("Numbers"));
166 fMonetaryBox
= new BBox(B_TRANSLATE("Currency"));
168 fDateBox
->SetLabel(B_TRANSLATE("Date"));
169 fTimeBox
->SetLabel(B_TRANSLATE("Time"));
170 fNumberBox
->SetLabel(B_TRANSLATE("Numbers"));
171 fMonetaryBox
->SetLabel(B_TRANSLATE("Currency"));
173 fDateBox
->AddChild(BLayoutBuilder::Grid
<>(spacing
, spacing
/ 2)
174 .SetInsets(spacing
, spacing
, spacing
, spacing
)
175 .Add(fullDateLabel
, 0, 0)
176 .Add(fFullDateExampleView
, 1, 0)
177 .Add(BSpaceLayoutItem::CreateGlue(), 2, 0)
178 .Add(longDateLabel
, 0, 1)
179 .Add(fLongDateExampleView
, 1, 1)
180 .Add(BSpaceLayoutItem::CreateGlue(), 2, 1)
181 .Add(mediumDateLabel
, 0, 2)
182 .Add(fMediumDateExampleView
, 1, 2)
183 .Add(BSpaceLayoutItem::CreateGlue(), 2, 2)
184 .Add(shortDateLabel
, 0, 3)
185 .Add(fShortDateExampleView
, 1, 3)
186 .Add(BSpaceLayoutItem::CreateGlue(), 2, 3)
189 fTimeBox
->AddChild(BLayoutBuilder::Grid
<>(spacing
, spacing
/ 2)
190 .SetInsets(spacing
, spacing
, spacing
, spacing
)
191 .AddGroup(B_HORIZONTAL
, spacing
, 0, 0, 2)
192 .Add(f24HourRadioButton
, 0)
193 .Add(f12HourRadioButton
, 0)
196 .Add(fullTimeLabel
, 0, 1)
197 .Add(fFullTimeExampleView
, 1, 1)
198 .Add(BSpaceLayoutItem::CreateGlue(), 2, 1)
199 .Add(longTimeLabel
, 0, 2)
200 .Add(fLongTimeExampleView
, 1, 2)
201 .Add(BSpaceLayoutItem::CreateGlue(), 2, 2)
202 .Add(mediumTimeLabel
, 0, 3)
203 .Add(fMediumTimeExampleView
, 1, 3)
204 .Add(BSpaceLayoutItem::CreateGlue(), 2, 3)
205 .Add(shortTimeLabel
, 0, 4)
206 .Add(fShortTimeExampleView
, 1, 4)
207 .Add(BSpaceLayoutItem::CreateGlue(), 2, 4)
210 fNumberBox
->AddChild(BLayoutBuilder::Grid
<>(spacing
, spacing
/ 2)
211 .SetInsets(spacing
, spacing
, spacing
, spacing
)
212 .Add(positiveNumberLabel
, 0, 0)
213 .Add(fPositiveNumberExampleView
, 1, 0)
214 .Add(BSpaceLayoutItem::CreateGlue(), 2, 0)
215 .Add(negativeNumberLabel
, 0, 1)
216 .Add(fNegativeNumberExampleView
, 1, 1)
217 .Add(BSpaceLayoutItem::CreateGlue(), 2, 1)
220 fMonetaryBox
->AddChild(BLayoutBuilder::Grid
<>(spacing
, spacing
/ 2)
221 .SetInsets(spacing
, spacing
, spacing
, spacing
)
222 .Add(positiveMonetaryLabel
, 0, 0)
223 .Add(fPositiveMonetaryExampleView
, 1, 0)
224 .Add(BSpaceLayoutItem::CreateGlue(), 2, 0)
225 .Add(negativeMonetaryLabel
, 0, 1)
226 .Add(fNegativeMonetaryExampleView
, 1, 1)
227 .Add(BSpaceLayoutItem::CreateGlue(), 2, 1)
230 BGroupLayout
* rootLayout
= new BGroupLayout(B_VERTICAL
, spacing
);
231 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR
));
232 SetLayout(rootLayout
);
233 BLayoutBuilder::Group
<>(rootLayout
)
234 .Add(fUseLanguageStringsCheckBox
)
237 .AddGroup(B_HORIZONTAL
, spacing
)
246 FormatSettingsView::~FormatSettingsView()
252 FormatSettingsView::AttachedToWindow()
254 fUseLanguageStringsCheckBox
->SetTarget(this);
255 f24HourRadioButton
->SetTarget(this);
256 f12HourRadioButton
->SetTarget(this);
261 FormatSettingsView::MessageReceived(BMessage
* message
)
263 switch (message
->what
) {
264 case B_LOCALE_CHANGED
:
266 // Time updated 12/24 hour clock
267 BFormattingConventions conventions
;
268 BLocale::Default()->GetFormattingConventions(&conventions
);
269 if (conventions
.Use24HourClock())
270 f24HourRadioButton
->SetValue(B_CONTROL_ON
);
272 f12HourRadioButton
->SetValue(B_CONTROL_ON
);
275 Window()->PostMessage(kMsgSettingsChanged
);
279 case kClockFormatChange
:
281 BFormattingConventions conventions
;
282 BLocale::Default()->GetFormattingConventions(&conventions
);
283 conventions
.SetExplicitUse24HourClock(
284 f24HourRadioButton
->Value() == B_CONTROL_ON
);
285 MutableLocaleRoster::Default()->SetDefaultFormattingConventions(
289 Window()->PostMessage(kMsgSettingsChanged
);
293 case kStringsLanguageChange
:
295 BFormattingConventions conventions
;
296 BLocale::Default()->GetFormattingConventions(&conventions
);
297 conventions
.SetUseStringsFromPreferredLanguage(
298 fUseLanguageStringsCheckBox
->Value() ? true : false);
299 MutableLocaleRoster::Default()->SetDefaultFormattingConventions(
304 Window()->PostMessage(kMsgSettingsChanged
);
309 BView::MessageReceived(message
);
315 FormatSettingsView::Revert()
317 MutableLocaleRoster::Default()->SetDefaultFormattingConventions(
318 fInitialConventions
);
325 FormatSettingsView::Refresh(bool setInitial
)
327 BFormattingConventions conventions
;
328 BLocale::Default()->GetFormattingConventions(&conventions
);
330 fInitialConventions
= conventions
;
332 if (!conventions
.Use24HourClock()) {
333 f12HourRadioButton
->SetValue(B_CONTROL_ON
);
334 fLocaleIs24Hour
= false;
336 f24HourRadioButton
->SetValue(B_CONTROL_ON
);
337 fLocaleIs24Hour
= true;
340 fUseLanguageStringsCheckBox
->SetValue(
341 conventions
.UseStringsFromPreferredLanguage()
342 ? B_CONTROL_ON
: B_CONTROL_OFF
);
348 // Return true if the Revert button should be enabled (i.e. something has been
351 FormatSettingsView::IsReversible() const
353 BFormattingConventions conventions
;
354 BLocale::Default()->GetFormattingConventions(&conventions
);
356 return conventions
!= fInitialConventions
;
361 FormatSettingsView::_UpdateExamples()
363 time_t timeValue
= (time_t)time(NULL
);
366 // Do NOT make these class members. We do want to recreate it everytime, as
367 // to get the updated settings from the locale roster.
368 BDateFormat dateFormat
;
369 BTimeFormat timeFormat
;
370 BNumberFormat numberFormat
;
372 dateFormat
.Format(result
, timeValue
, B_FULL_DATE_FORMAT
);
373 fFullDateExampleView
->SetText(result
);
375 dateFormat
.Format(result
, timeValue
, B_LONG_DATE_FORMAT
);
376 fLongDateExampleView
->SetText(result
);
378 dateFormat
.Format(result
, timeValue
, B_MEDIUM_DATE_FORMAT
);
379 fMediumDateExampleView
->SetText(result
);
381 dateFormat
.Format(result
, timeValue
, B_SHORT_DATE_FORMAT
);
382 fShortDateExampleView
->SetText(result
);
384 timeFormat
.Format(result
, timeValue
, B_FULL_TIME_FORMAT
);
385 fFullTimeExampleView
->SetText(result
);
387 timeFormat
.Format(result
, timeValue
, B_LONG_TIME_FORMAT
);
388 fLongTimeExampleView
->SetText(result
);
390 timeFormat
.Format(result
, timeValue
, B_MEDIUM_TIME_FORMAT
);
391 fMediumTimeExampleView
->SetText(result
);
393 timeFormat
.Format(result
, timeValue
, B_SHORT_TIME_FORMAT
);
394 fShortTimeExampleView
->SetText(result
);
396 status_t status
= numberFormat
.Format(result
, 1234.5678);
398 fPositiveNumberExampleView
->SetText(result
);
400 fPositiveNumberExampleView
->SetText("ERROR");
402 status
= numberFormat
.Format(result
, -1234.5678);
404 fNegativeNumberExampleView
->SetText(result
);
406 fNegativeNumberExampleView
->SetText("ERROR");
408 status
= numberFormat
.FormatMonetary(result
, 1234.56);
410 fPositiveMonetaryExampleView
->SetText(result
);
412 fPositiveMonetaryExampleView
->SetText("ERROR");
414 status
= numberFormat
.FormatMonetary(result
, -1234.56);
416 fNegativeMonetaryExampleView
->SetText(result
);
418 fNegativeMonetaryExampleView
->SetText("ERROR");