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 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
232 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
233 .Add(fUseLanguageStringsCheckBox
)
236 .AddGroup(B_HORIZONTAL
, spacing
)
244 FormatSettingsView::~FormatSettingsView()
250 FormatSettingsView::AttachedToWindow()
252 fUseLanguageStringsCheckBox
->SetTarget(this);
253 f24HourRadioButton
->SetTarget(this);
254 f12HourRadioButton
->SetTarget(this);
259 FormatSettingsView::MessageReceived(BMessage
* message
)
261 switch (message
->what
) {
262 case B_LOCALE_CHANGED
:
264 // Time updated 12/24 hour clock
265 BFormattingConventions conventions
;
266 BLocale::Default()->GetFormattingConventions(&conventions
);
267 if (conventions
.Use24HourClock())
268 f24HourRadioButton
->SetValue(B_CONTROL_ON
);
270 f12HourRadioButton
->SetValue(B_CONTROL_ON
);
273 Window()->PostMessage(kMsgSettingsChanged
);
277 case kClockFormatChange
:
279 BFormattingConventions conventions
;
280 BLocale::Default()->GetFormattingConventions(&conventions
);
281 conventions
.SetExplicitUse24HourClock(
282 f24HourRadioButton
->Value() == B_CONTROL_ON
);
283 MutableLocaleRoster::Default()->SetDefaultFormattingConventions(
287 Window()->PostMessage(kMsgSettingsChanged
);
291 case kStringsLanguageChange
:
293 BFormattingConventions conventions
;
294 BLocale::Default()->GetFormattingConventions(&conventions
);
295 conventions
.SetUseStringsFromPreferredLanguage(
296 fUseLanguageStringsCheckBox
->Value() ? true : false);
297 MutableLocaleRoster::Default()->SetDefaultFormattingConventions(
302 Window()->PostMessage(kMsgSettingsChanged
);
307 BView::MessageReceived(message
);
313 FormatSettingsView::Revert()
315 MutableLocaleRoster::Default()->SetDefaultFormattingConventions(
316 fInitialConventions
);
323 FormatSettingsView::Refresh(bool setInitial
)
325 BFormattingConventions conventions
;
326 BLocale::Default()->GetFormattingConventions(&conventions
);
328 fInitialConventions
= conventions
;
330 if (!conventions
.Use24HourClock()) {
331 f12HourRadioButton
->SetValue(B_CONTROL_ON
);
332 fLocaleIs24Hour
= false;
334 f24HourRadioButton
->SetValue(B_CONTROL_ON
);
335 fLocaleIs24Hour
= true;
338 fUseLanguageStringsCheckBox
->SetValue(
339 conventions
.UseStringsFromPreferredLanguage()
340 ? B_CONTROL_ON
: B_CONTROL_OFF
);
346 // Return true if the Revert button should be enabled (i.e. something has been
349 FormatSettingsView::IsReversible() const
351 BFormattingConventions conventions
;
352 BLocale::Default()->GetFormattingConventions(&conventions
);
354 return conventions
!= fInitialConventions
;
359 FormatSettingsView::_UpdateExamples()
361 time_t timeValue
= (time_t)time(NULL
);
364 // Do NOT make these class members. We do want to recreate it everytime, as
365 // to get the updated settings from the locale roster.
366 BDateFormat dateFormat
;
367 BTimeFormat timeFormat
;
368 BNumberFormat numberFormat
;
370 dateFormat
.Format(result
, timeValue
, B_FULL_DATE_FORMAT
);
371 fFullDateExampleView
->SetText(result
);
373 dateFormat
.Format(result
, timeValue
, B_LONG_DATE_FORMAT
);
374 fLongDateExampleView
->SetText(result
);
376 dateFormat
.Format(result
, timeValue
, B_MEDIUM_DATE_FORMAT
);
377 fMediumDateExampleView
->SetText(result
);
379 dateFormat
.Format(result
, timeValue
, B_SHORT_DATE_FORMAT
);
380 fShortDateExampleView
->SetText(result
);
382 timeFormat
.Format(result
, timeValue
, B_FULL_TIME_FORMAT
);
383 fFullTimeExampleView
->SetText(result
);
385 timeFormat
.Format(result
, timeValue
, B_LONG_TIME_FORMAT
);
386 fLongTimeExampleView
->SetText(result
);
388 timeFormat
.Format(result
, timeValue
, B_MEDIUM_TIME_FORMAT
);
389 fMediumTimeExampleView
->SetText(result
);
391 timeFormat
.Format(result
, timeValue
, B_SHORT_TIME_FORMAT
);
392 fShortTimeExampleView
->SetText(result
);
394 status_t status
= numberFormat
.Format(result
, 1234.5678);
396 fPositiveNumberExampleView
->SetText(result
);
398 fPositiveNumberExampleView
->SetText("ERROR");
400 status
= numberFormat
.Format(result
, -1234.5678);
402 fNegativeNumberExampleView
->SetText(result
);
404 fNegativeNumberExampleView
->SetText("ERROR");
406 status
= numberFormat
.FormatMonetary(result
, 1234.56);
408 fPositiveMonetaryExampleView
->SetText(result
);
410 fPositiveMonetaryExampleView
->SetText("ERROR");
412 status
= numberFormat
.FormatMonetary(result
, -1234.56);
414 fNegativeMonetaryExampleView
->SetText(result
);
416 fNegativeMonetaryExampleView
->SetText("ERROR");