OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/options/system_page_view.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/stl_util-inl.h" | |
11 #include "base/string16.h" | |
12 #include "base/string_util.h" | |
13 #include "base/stringprintf.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "chrome/browser/chromeos/cros/cros_library.h" | |
16 #include "chrome/browser/chromeos/cros/keyboard_library.h" | |
17 #include "chrome/browser/chromeos/cros/system_library.h" | |
18 #include "chrome/browser/chromeos/language_preferences.h" | |
19 #include "chrome/browser/chromeos/options/options_window_view.h" | |
20 #include "chrome/browser/prefs/pref_member.h" | |
21 #include "chrome/browser/profiles/profile.h" | |
22 #include "chrome/common/pref_names.h" | |
23 #include "grit/generated_resources.h" | |
24 #include "ui/base/l10n/l10n_util.h" | |
25 #include "ui/base/models/combobox_model.h" | |
26 #include "unicode/timezone.h" | |
27 #include "views/controls/button/checkbox.h" | |
28 #include "views/controls/button/native_button.h" | |
29 #include "views/controls/combobox/combobox.h" | |
30 #include "views/controls/slider/slider.h" | |
31 #include "views/layout/layout_constants.h" | |
32 #include "views/window/window.h" | |
33 | |
34 namespace chromeos { | |
35 | |
36 //////////////////////////////////////////////////////////////////////////////// | |
37 // DateTimeSection | |
38 | |
39 // Date/Time section for datetime settings | |
40 class DateTimeSection : public SettingsPageSection, | |
41 public views::Combobox::Listener, | |
42 public SystemLibrary::Observer { | |
43 public: | |
44 explicit DateTimeSection(Profile* profile); | |
45 virtual ~DateTimeSection(); | |
46 | |
47 // Overridden from views::Combobox::Listener: | |
48 virtual void ItemChanged(views::Combobox* sender, | |
49 int prev_index, | |
50 int new_index); | |
51 | |
52 // Overridden from SystemLibrary::Observer: | |
53 virtual void TimezoneChanged(const icu::TimeZone& timezone); | |
54 | |
55 protected: | |
56 // SettingsPageSection overrides: | |
57 virtual void InitContents(GridLayout* layout); | |
58 | |
59 private: | |
60 // The combobox model for the list of timezones. | |
61 class TimezoneComboboxModel : public ui::ComboboxModel { | |
62 public: | |
63 TimezoneComboboxModel() { | |
64 // TODO(chocobo): For now, add all the GMT timezones. | |
65 // We may eventually want to use icu::TimeZone::createEnumeration() | |
66 // to list all the timezones and pick the ones we want to show. | |
67 // NOTE: This currently does not handle daylight savings properly | |
68 // b/c this is just a manually selected list of timezones that | |
69 // happen to span the GMT-11 to GMT+12 Today. When daylight savings | |
70 // kick in, this list might have more than one timezone in the same | |
71 // GMT bucket. | |
72 timezones_.push_back(icu::TimeZone::createTimeZone( | |
73 icu::UnicodeString::fromUTF8("Pacific/Samoa"))); | |
74 timezones_.push_back(icu::TimeZone::createTimeZone( | |
75 icu::UnicodeString::fromUTF8("US/Hawaii"))); | |
76 timezones_.push_back(icu::TimeZone::createTimeZone( | |
77 icu::UnicodeString::fromUTF8("US/Alaska"))); | |
78 timezones_.push_back(icu::TimeZone::createTimeZone( | |
79 icu::UnicodeString::fromUTF8("US/Pacific"))); | |
80 timezones_.push_back(icu::TimeZone::createTimeZone( | |
81 icu::UnicodeString::fromUTF8("US/Mountain"))); | |
82 timezones_.push_back(icu::TimeZone::createTimeZone( | |
83 icu::UnicodeString::fromUTF8("US/Central"))); | |
84 timezones_.push_back(icu::TimeZone::createTimeZone( | |
85 icu::UnicodeString::fromUTF8("US/Eastern"))); | |
86 timezones_.push_back(icu::TimeZone::createTimeZone( | |
87 icu::UnicodeString::fromUTF8("America/Santiago"))); | |
88 timezones_.push_back(icu::TimeZone::createTimeZone( | |
89 icu::UnicodeString::fromUTF8("America/Sao_Paulo"))); | |
90 timezones_.push_back(icu::TimeZone::createTimeZone( | |
91 icu::UnicodeString::fromUTF8("Atlantic/South_Georgia"))); | |
92 timezones_.push_back(icu::TimeZone::createTimeZone( | |
93 icu::UnicodeString::fromUTF8("Atlantic/Cape_Verde"))); | |
94 timezones_.push_back(icu::TimeZone::createTimeZone( | |
95 icu::UnicodeString::fromUTF8("Europe/London"))); | |
96 timezones_.push_back(icu::TimeZone::createTimeZone( | |
97 icu::UnicodeString::fromUTF8("Europe/Rome"))); | |
98 timezones_.push_back(icu::TimeZone::createTimeZone( | |
99 icu::UnicodeString::fromUTF8("Europe/Helsinki"))); | |
100 timezones_.push_back(icu::TimeZone::createTimeZone( | |
101 icu::UnicodeString::fromUTF8("Europe/Moscow"))); | |
102 timezones_.push_back(icu::TimeZone::createTimeZone( | |
103 icu::UnicodeString::fromUTF8("Asia/Dubai"))); | |
104 timezones_.push_back(icu::TimeZone::createTimeZone( | |
105 icu::UnicodeString::fromUTF8("Asia/Karachi"))); | |
106 timezones_.push_back(icu::TimeZone::createTimeZone( | |
107 icu::UnicodeString::fromUTF8("Asia/Dhaka"))); | |
108 timezones_.push_back(icu::TimeZone::createTimeZone( | |
109 icu::UnicodeString::fromUTF8("Asia/Bangkok"))); | |
110 timezones_.push_back(icu::TimeZone::createTimeZone( | |
111 icu::UnicodeString::fromUTF8("Asia/Hong_Kong"))); | |
112 timezones_.push_back(icu::TimeZone::createTimeZone( | |
113 icu::UnicodeString::fromUTF8("Asia/Tokyo"))); | |
114 timezones_.push_back(icu::TimeZone::createTimeZone( | |
115 icu::UnicodeString::fromUTF8("Australia/Sydney"))); | |
116 timezones_.push_back(icu::TimeZone::createTimeZone( | |
117 icu::UnicodeString::fromUTF8("Asia/Magadan"))); | |
118 timezones_.push_back(icu::TimeZone::createTimeZone( | |
119 icu::UnicodeString::fromUTF8("Pacific/Auckland"))); | |
120 } | |
121 | |
122 virtual ~TimezoneComboboxModel() { | |
123 STLDeleteElements(&timezones_); | |
124 } | |
125 | |
126 virtual int GetItemCount() { | |
127 return static_cast<int>(timezones_.size()); | |
128 } | |
129 | |
130 virtual string16 GetItemAt(int index) { | |
131 icu::UnicodeString name; | |
132 timezones_[index]->getDisplayName(name); | |
133 std::wstring output; | |
134 UTF16ToWide(name.getBuffer(), name.length(), &output); | |
135 int hour_offset = timezones_[index]->getRawOffset() / 3600000; | |
136 return WideToUTF16Hack( | |
137 base::StringPrintf(hour_offset == 0 ? L"(GMT) " : (hour_offset > 0 ? | |
138 L"(GMT+%d) " : L"(GMT%d) "), hour_offset) + output); | |
139 } | |
140 | |
141 virtual icu::TimeZone* GetTimeZoneAt(int index) { | |
142 return timezones_[index]; | |
143 } | |
144 | |
145 private: | |
146 std::vector<icu::TimeZone*> timezones_; | |
147 | |
148 DISALLOW_COPY_AND_ASSIGN(TimezoneComboboxModel); | |
149 }; | |
150 | |
151 // TimeZone combobox model. | |
152 views::Combobox* timezone_combobox_; | |
153 | |
154 // Controls for this section: | |
155 TimezoneComboboxModel timezone_combobox_model_; | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(DateTimeSection); | |
158 }; | |
159 | |
160 DateTimeSection::DateTimeSection(Profile* profile) | |
161 : SettingsPageSection(profile, IDS_OPTIONS_SETTINGS_SECTION_TITLE_DATETIME), | |
162 timezone_combobox_(NULL) { | |
163 CrosLibrary::Get()->GetSystemLibrary()->AddObserver(this); | |
164 } | |
165 | |
166 DateTimeSection::~DateTimeSection() { | |
167 CrosLibrary::Get()->GetSystemLibrary()->RemoveObserver(this); | |
168 } | |
169 | |
170 void DateTimeSection::ItemChanged(views::Combobox* sender, | |
171 int prev_index, | |
172 int new_index) { | |
173 if (new_index == prev_index) | |
174 return; | |
175 | |
176 CrosLibrary::Get()->GetSystemLibrary()->SetTimezone( | |
177 timezone_combobox_model_.GetTimeZoneAt(new_index)); | |
178 } | |
179 | |
180 void DateTimeSection::TimezoneChanged(const icu::TimeZone& timezone) { | |
181 for (int i = 0; i < timezone_combobox_model_.GetItemCount(); i++) { | |
182 if (*timezone_combobox_model_.GetTimeZoneAt(i) == timezone) { | |
183 timezone_combobox_->SetSelectedItem(i); | |
184 return; | |
185 } | |
186 } | |
187 } | |
188 | |
189 void DateTimeSection::InitContents(GridLayout* layout) { | |
190 timezone_combobox_ = new views::Combobox(&timezone_combobox_model_); | |
191 timezone_combobox_->set_listener(this); | |
192 | |
193 layout->StartRow(0, double_column_view_set_id()); | |
194 layout->AddView(new views::Label(UTF16ToWide( | |
195 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_TIMEZONE_DESCRIPTION)))); | |
196 layout->AddView(timezone_combobox_); | |
197 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
198 | |
199 TimezoneChanged(CrosLibrary::Get()->GetSystemLibrary()->GetTimezone()); | |
200 } | |
201 | |
202 //////////////////////////////////////////////////////////////////////////////// | |
203 // TouchpadSection | |
204 | |
205 class TouchpadSection : public SettingsPageSection, | |
206 public views::ButtonListener, | |
207 public views::SliderListener { | |
208 public: | |
209 explicit TouchpadSection(Profile* profile); | |
210 virtual ~TouchpadSection() {} | |
211 | |
212 // Overridden from views::ButtonListener: | |
213 virtual void ButtonPressed(views::Button* sender, const views::Event& event); | |
214 | |
215 // Overridden from views::SliderListener: | |
216 virtual void SliderValueChanged(views::Slider* sender); | |
217 | |
218 protected: | |
219 // SettingsPageSection overrides: | |
220 virtual void InitContents(GridLayout* layout); | |
221 virtual void NotifyPrefChanged(const std::string* pref_name); | |
222 | |
223 private: | |
224 // The View that contains the contents of the section. | |
225 views::View* contents_; | |
226 | |
227 // Controls for this section: | |
228 views::Checkbox* enable_tap_to_click_checkbox_; | |
229 views::Slider* sensitivity_slider_; | |
230 | |
231 // Preferences for this section: | |
232 BooleanPrefMember tap_to_click_enabled_; | |
233 IntegerPrefMember sensitivity_; | |
234 | |
235 DISALLOW_COPY_AND_ASSIGN(TouchpadSection); | |
236 }; | |
237 | |
238 TouchpadSection::TouchpadSection(Profile* profile) | |
239 : SettingsPageSection(profile, IDS_OPTIONS_SETTINGS_SECTION_TITLE_TOUCHPAD), | |
240 enable_tap_to_click_checkbox_(NULL), | |
241 sensitivity_slider_(NULL) { | |
242 } | |
243 | |
244 void TouchpadSection::ButtonPressed( | |
245 views::Button* sender, const views::Event& event) { | |
246 if (sender == enable_tap_to_click_checkbox_) { | |
247 bool enabled = enable_tap_to_click_checkbox_->checked(); | |
248 UserMetricsRecordAction(enabled ? | |
249 UserMetricsAction("Options_TapToClickCheckbox_Enable") : | |
250 UserMetricsAction("Options_TapToClickCheckbox_Disable"), | |
251 profile()->GetPrefs()); | |
252 tap_to_click_enabled_.SetValue(enabled); | |
253 } | |
254 } | |
255 | |
256 void TouchpadSection::SliderValueChanged(views::Slider* sender) { | |
257 if (sender == sensitivity_slider_) { | |
258 double value = sensitivity_slider_->value(); | |
259 UserMetricsRecordAction( | |
260 UserMetricsAction("Options_SensitivitySlider_Changed"), | |
261 profile()->GetPrefs()); | |
262 sensitivity_.SetValue(value); | |
263 } | |
264 } | |
265 | |
266 void TouchpadSection::InitContents(GridLayout* layout) { | |
267 enable_tap_to_click_checkbox_ = | |
268 new views::Checkbox(UTF16ToWide(l10n_util::GetStringUTF16( | |
269 IDS_OPTIONS_SETTINGS_TAP_TO_CLICK_ENABLED_DESCRIPTION))); | |
270 enable_tap_to_click_checkbox_->set_listener(this); | |
271 enable_tap_to_click_checkbox_->SetMultiLine(true); | |
272 // Create sensitivity slider with values between 1 and 5 step 1 | |
273 sensitivity_slider_ = new views::Slider(1, 5, 1, | |
274 static_cast<views::Slider::StyleFlags>( | |
275 views::Slider::STYLE_DRAW_VALUE | | |
276 views::Slider::STYLE_UPDATE_ON_RELEASE), | |
277 this); | |
278 | |
279 layout->StartRow(0, double_column_view_set_id()); | |
280 layout->AddView(new views::Label(UTF16ToWide(l10n_util::GetStringUTF16( | |
281 IDS_OPTIONS_SETTINGS_SENSITIVITY_DESCRIPTION)))); | |
282 layout->AddView(sensitivity_slider_); | |
283 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
284 layout->StartRow(0, single_column_view_set_id()); | |
285 layout->AddView(enable_tap_to_click_checkbox_); | |
286 layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing); | |
287 | |
288 // Init member prefs so we can update the controls if prefs change. | |
289 tap_to_click_enabled_.Init(prefs::kTapToClickEnabled, | |
290 profile()->GetPrefs(), this); | |
291 sensitivity_.Init(prefs::kTouchpadSensitivity, | |
292 profile()->GetPrefs(), this); | |
293 } | |
294 | |
295 void TouchpadSection::NotifyPrefChanged(const std::string* pref_name) { | |
296 if (!pref_name || *pref_name == prefs::kTapToClickEnabled) { | |
297 bool enabled = tap_to_click_enabled_.GetValue(); | |
298 enable_tap_to_click_checkbox_->SetChecked(enabled); | |
299 } | |
300 if (!pref_name || *pref_name == prefs::kTouchpadSensitivity) { | |
301 double value = sensitivity_.GetValue(); | |
302 sensitivity_slider_->SetValue(value); | |
303 } | |
304 } | |
305 | |
306 /////////////////////////////////////////////////////////////////////////////// | |
307 // AccessibilitySection | |
308 | |
309 // Checkbox for specifying if accessibility should be enabled for this profile | |
310 class AccessibilitySection : public SettingsPageSection, | |
311 public views::ButtonListener { | |
312 public: | |
313 explicit AccessibilitySection(Profile* profile); | |
314 virtual ~AccessibilitySection() {} | |
315 | |
316 protected: | |
317 // Overridden from views::ButtonListener: | |
318 virtual void ButtonPressed(views::Button* sender, | |
319 const views::Event& event); | |
320 | |
321 // Overridden from SettingsPageSection: | |
322 virtual void InitContents(GridLayout* layout); | |
323 virtual void NotifyPrefChanged(const std::string* pref_name); | |
324 | |
325 private: | |
326 // The View that contains the contents of the section. | |
327 views::View* contents_; | |
328 | |
329 // Controls for this section: | |
330 views::Checkbox* accessibility_checkbox_; | |
331 | |
332 // Preferences for this section: | |
333 BooleanPrefMember accessibility_enabled_; | |
334 | |
335 DISALLOW_COPY_AND_ASSIGN(AccessibilitySection); | |
336 }; | |
337 | |
338 AccessibilitySection::AccessibilitySection(Profile* profile) | |
339 : SettingsPageSection(profile, | |
340 IDS_OPTIONS_SETTINGS_SECTION_TITLE_ACCESSIBILITY), | |
341 accessibility_checkbox_(NULL) { | |
342 } | |
343 | |
344 void AccessibilitySection::InitContents(GridLayout* layout) { | |
345 accessibility_checkbox_ = | |
346 new views::Checkbox(UTF16ToWide(l10n_util::GetStringUTF16( | |
347 IDS_OPTIONS_SETTINGS_ACCESSIBILITY_DESCRIPTION))); | |
348 accessibility_checkbox_->set_listener(this); | |
349 accessibility_checkbox_->SetMultiLine(true); | |
350 | |
351 layout->StartRow(0, double_column_view_set_id()); | |
352 layout->AddView(accessibility_checkbox_); | |
353 layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing); | |
354 | |
355 // Init member prefs so we can update the controls if prefs change. | |
356 accessibility_enabled_.Init(prefs::kAccessibilityEnabled, | |
357 profile()->GetPrefs(), this); | |
358 } | |
359 | |
360 void AccessibilitySection::ButtonPressed( | |
361 views::Button* sender, const views::Event& event) { | |
362 if (sender == accessibility_checkbox_) { | |
363 bool enabled = accessibility_checkbox_->checked(); | |
364 // Set the accessibility enabled value in profile/prefs | |
365 accessibility_enabled_.SetValue(enabled); | |
366 } | |
367 } | |
368 | |
369 void AccessibilitySection::NotifyPrefChanged(const std::string* pref_name) { | |
370 if (!pref_name || *pref_name == prefs::kAccessibilityEnabled) { | |
371 bool enabled = accessibility_enabled_.GetValue(); | |
372 accessibility_checkbox_->SetChecked(enabled); | |
373 } | |
374 } | |
375 | |
376 //////////////////////////////////////////////////////////////////////////////// | |
377 // SystemPageView | |
378 | |
379 //////////////////////////////////////////////////////////////////////////////// | |
380 // SystemPageView, SettingsPageView implementation: | |
381 | |
382 void SystemPageView::InitControlLayout() { | |
383 GridLayout* layout = GridLayout::CreatePanel(this); | |
384 SetLayoutManager(layout); | |
385 | |
386 int single_column_view_set_id = 0; | |
387 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id); | |
388 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
389 GridLayout::USE_PREF, 0, 0); | |
390 | |
391 layout->StartRow(0, single_column_view_set_id); | |
392 layout->AddView(new DateTimeSection(profile())); | |
393 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
394 layout->StartRow(0, single_column_view_set_id); | |
395 layout->AddView(new TouchpadSection(profile())); | |
396 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
397 layout->StartRow(0, single_column_view_set_id); | |
398 layout->AddView(new AccessibilitySection(profile())); | |
399 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | |
400 } | |
401 | |
402 } // namespace chromeos | |
OLD | NEW |