OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/language_hangul_config_view.h" | |
6 | |
7 #include "app/l10n_util.h" | |
8 #include "base/string16.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/chromeos/cros/cros_library.h" | |
11 #include "chrome/browser/chromeos/cros/input_method_library.h" | |
12 #include "chrome/browser/chromeos/language_preferences.h" | |
13 #include "chrome/browser/chromeos/preferences.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/notification_type.h" | |
16 #include "chrome/common/pref_names.h" | |
17 #include "grit/generated_resources.h" | |
18 #include "grit/locale_settings.h" | |
19 #include "ui/base/models/combobox_model.h" | |
20 #include "views/controls/button/checkbox.h" | |
21 #include "views/controls/label.h" | |
22 #include "views/grid_layout.h" | |
23 #include "views/standard_layout.h" | |
24 #include "views/window/window.h" | |
25 | |
26 namespace chromeos { | |
27 | |
28 // The combobox model for the list of hangul keyboards. | |
29 class HangulKeyboardComboboxModel : public ui::ComboboxModel { | |
30 public: | |
31 HangulKeyboardComboboxModel() { | |
32 for (size_t i = 0; i < language_prefs::kNumHangulKeyboardNameIDPairs; | |
33 ++i) { | |
34 layouts_.push_back(std::make_pair( | |
35 l10n_util::GetStringUTF8( | |
36 language_prefs::kHangulKeyboardNameIDPairs[i].message_id), | |
37 language_prefs::kHangulKeyboardNameIDPairs[i].keyboard_id)); | |
38 } | |
39 } | |
40 | |
41 // Implements ui::ComboboxModel interface. | |
42 virtual int GetItemCount() { | |
43 return static_cast<int>(layouts_.size()); | |
44 } | |
45 | |
46 // Implements ui::ComboboxModel interface. | |
47 virtual string16 GetItemAt(int index) { | |
48 if (index < 0 || index > GetItemCount()) { | |
49 LOG(ERROR) << "Index is out of bounds: " << index; | |
50 return string16(); | |
51 } | |
52 return UTF8ToUTF16(layouts_.at(index).first); | |
53 } | |
54 | |
55 // Gets a keyboard layout ID (e.g. "2", "3f", ..) for an item at zero-origin | |
56 // |index|. This function is NOT part of the ComboboxModel interface. | |
57 std::string GetItemIDAt(int index) { | |
58 if (index < 0 || index > GetItemCount()) { | |
59 LOG(ERROR) << "Index is out of bounds: " << index; | |
60 return ""; | |
61 } | |
62 return layouts_.at(index).second; | |
63 } | |
64 | |
65 // Gets an index (>= 0) of an item whose keyboard layout ID is |layout_ld|. | |
66 // Returns -1 if such item is not found. This function is NOT part of the | |
67 // ComboboxModel interface. | |
68 int GetIndexFromID(const std::string& layout_id) { | |
69 for (size_t i = 0; i < layouts_.size(); ++i) { | |
70 if (GetItemIDAt(i) == layout_id) { | |
71 return static_cast<int>(i); | |
72 } | |
73 } | |
74 return -1; | |
75 } | |
76 | |
77 private: | |
78 std::vector<std::pair<std::string, std::string> > layouts_; | |
79 DISALLOW_COPY_AND_ASSIGN(HangulKeyboardComboboxModel); | |
80 }; | |
81 | |
82 LanguageHangulConfigView::LanguageHangulConfigView(Profile* profile) | |
83 : OptionsPageView(profile), | |
84 contents_(NULL), | |
85 hangul_keyboard_combobox_(NULL), | |
86 hangul_keyboard_combobox_model_(new HangulKeyboardComboboxModel) { | |
87 keyboard_pref_.Init( | |
88 prefs::kLanguageHangulKeyboard, profile->GetPrefs(), this); | |
89 } | |
90 | |
91 LanguageHangulConfigView::~LanguageHangulConfigView() { | |
92 } | |
93 | |
94 void LanguageHangulConfigView::ItemChanged( | |
95 views::Combobox* sender, int prev_index, int new_index) { | |
96 const std::string id = | |
97 hangul_keyboard_combobox_model_->GetItemIDAt(new_index); | |
98 VLOG(1) << "Changing Hangul keyboard pref to " << id; | |
99 keyboard_pref_.SetValue(id); | |
100 } | |
101 | |
102 void LanguageHangulConfigView::Layout() { | |
103 // Not sure why but this is needed to show contents in the dialog. | |
104 contents_->SetBounds(0, 0, width(), height()); | |
105 } | |
106 | |
107 int LanguageHangulConfigView::GetDialogButtons() const { | |
108 return MessageBoxFlags::DIALOGBUTTON_OK; | |
109 } | |
110 | |
111 std::wstring LanguageHangulConfigView::GetDialogButtonLabel( | |
112 MessageBoxFlags::DialogButton button) const { | |
113 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { | |
114 return UTF16ToWide(l10n_util::GetStringUTF16(IDS_OK)); | |
115 } | |
116 return L""; | |
117 } | |
118 | |
119 std::wstring LanguageHangulConfigView::GetWindowTitle() const { | |
120 return UTF16ToWide(l10n_util::GetStringUTF16( | |
121 IDS_OPTIONS_SETTINGS_LANGUAGES_HANGUL_SETTINGS_TITLE)); | |
122 } | |
123 | |
124 gfx::Size LanguageHangulConfigView::GetPreferredSize() { | |
125 // TODO(satorux): Create our own localized content size once the UI is done. | |
126 return gfx::Size(views::Window::GetLocalizedContentsSize( | |
127 IDS_LANGUAGES_INPUT_DIALOG_WIDTH_CHARS, | |
128 IDS_LANGUAGES_INPUT_DIALOG_HEIGHT_LINES)); | |
129 } | |
130 | |
131 void LanguageHangulConfigView::InitControlLayout() { | |
132 using views::ColumnSet; | |
133 using views::GridLayout; | |
134 | |
135 contents_ = new views::View; | |
136 AddChildView(contents_); | |
137 | |
138 GridLayout* layout = new GridLayout(contents_); | |
139 layout->SetInsets(kPanelVertMargin, kPanelHorizMargin, | |
140 kPanelVertMargin, kPanelHorizMargin); | |
141 contents_->SetLayoutManager(layout); | |
142 | |
143 const int kColumnSetId = 0; | |
144 ColumnSet* column_set = layout->AddColumnSet(kColumnSetId); | |
145 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
146 GridLayout::USE_PREF, 0, 0); | |
147 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | |
148 column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0, | |
149 GridLayout::USE_PREF, 0, 0); | |
150 layout->StartRow(0, kColumnSetId); | |
151 | |
152 // Settings for the Hangul IME. | |
153 layout->AddView(new views::Label(UTF16ToWide( | |
154 l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_KEYBOARD_LAYOUT_TEXT)))); | |
155 | |
156 hangul_keyboard_combobox_ | |
157 = new views::Combobox(hangul_keyboard_combobox_model_.get()); | |
158 hangul_keyboard_combobox_->set_listener(this); | |
159 | |
160 // Initialize the combobox to what's saved in user preferences. Otherwise, | |
161 // ItemChanged() will be called with |new_index| == 0. | |
162 NotifyPrefChanged(); | |
163 layout->AddView(hangul_keyboard_combobox_); | |
164 } | |
165 | |
166 void LanguageHangulConfigView::Observe(NotificationType type, | |
167 const NotificationSource& source, | |
168 const NotificationDetails& details) { | |
169 if (type == NotificationType::PREF_CHANGED) { | |
170 // Observe(PREF_CHANGED) is called when the chromeos::Preferences object | |
171 // changed the prefs::kLanguageHangulKeyboard preference. Note that this | |
172 // function is NOT called when this dialog calls keyboard_pref_.SetValue(). | |
173 NotifyPrefChanged(); | |
174 } | |
175 } | |
176 | |
177 void LanguageHangulConfigView::NotifyPrefChanged() { | |
178 const std::string id = keyboard_pref_.GetValue(); | |
179 const int index = | |
180 hangul_keyboard_combobox_model_->GetIndexFromID(id); | |
181 if (index >= 0) | |
182 hangul_keyboard_combobox_->SetSelectedItem(index); | |
183 } | |
184 | |
185 } // namespace chromeos | |
OLD | NEW |