1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/status/language_menu_button.h"
#include <string>
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/status/status_area_host.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
// The language menu consists of 3 parts (in this order):
//
// (1) XKB layout names and IME languages names. The size of the list is
// always >= 1.
// (2) IME properties. This list might be empty.
// (3) "Configure IME..." button.
//
// Example of the menu (Japanese):
//
// ============================== (border of the popup window)
// [ ] US (|index| in the following functions is 0)
// [*] Anthy
// [ ] PinYin
// ------------------------------ (separator)
// [*] Hiragana (index = 5, The property has 2 radio groups)
// [ ] Katakana
// [ ] HalfWidthKatakana
// [*] Roman
// [ ] Kana
// ------------------------------ (separator)
// Configure IME... (index = 11)
// ============================== (border of the popup window)
//
// Example of the menu (Simplified Chinese):
//
// ============================== (border of the popup window)
// [ ] US
// [ ] Anthy
// [*] PinYin
// ------------------------------ (separator)
// Switch to full letter mode (The property has 2 command buttons)
// Switch to half punctuation mode
// ------------------------------ (separator)
// Configure IME...
// ============================== (border of the popup window)
//
namespace {
// Constants to specify the type of items in |model_|.
enum {
COMMAND_ID_LANGUAGES = 0, // US, Anthy, PinYin, ...
COMMAND_ID_IME_PROPERTIES, // Hiragana, Katakana, ...
COMMAND_ID_CONFIGURE_IME, // The "Configure IME..." button.
};
// A group ID for IME properties starts from 0. We use the huge value for the
// XKB/IME language list to avoid conflict.
const int kRadioGroupLanguage = 1 << 16;
const int kRadioGroupNone = -1;
const size_t kMaxLanguageNameLen = 7;
const wchar_t kSpacer[] = L"MMMMMMM";
// Returns the language name for the given |language|. Instead of input
// method names like "Pinyin" and "Anthy", we'll show language names like
// "Chinese (Simplified)" and "Japanese".
std::string GetLanguageName(const chromeos::InputLanguage& language) {
std::string language_code = language.language_code;
if (language.id == "pinyin") {
// The pinyin input method returns "zh_CN" as language_code, but
// l10n_util expects "zh-CN".
language_code = "zh-CN";
} else if (language.id == "chewing") {
// Likewise, the chewing input method returns "zh" as language_code,
// which is ambiguous. We use zh-TW instead.
language_code = "zh-TW";
} else if (language_code == "t") {
// "t" is used by input methods that do not associate with a
// particular language. Returns the display name as-is.
return language.display_name;
}
const string16 language_name = l10n_util::GetDisplayNameForLocale(
language_code,
g_browser_process->GetApplicationLocale(),
true);
// TODO(satorux): We should add input method names if multiple input
// methods are available for one input language.
return UTF16ToUTF8(language_name);
}
// Converts chromeos::InputLanguage object into human readable string. Returns
// a string for the drop-down menu if |for_menu| is true. Otherwise, returns a
// string for the status area.
std::string FormatInputLanguage(
const chromeos::InputLanguage& language, bool for_menu) {
std::string formatted = GetLanguageName(language);
if (formatted.empty()) {
formatted = language.id;
}
if (!for_menu) {
// For status area. Trim the string.
formatted = formatted.substr(0, kMaxLanguageNameLen);
// TODO(yusukes): Simple substr() does not work for non-ASCII string.
// TODO(yusukes): How can we ensure that the trimmed string does not
// overflow the area?
}
return formatted;
}
} // namespace
namespace chromeos {
////////////////////////////////////////////////////////////////////////////////
// LanguageMenuButton
LanguageMenuButton::LanguageMenuButton(StatusAreaHost* host)
: MenuButton(NULL, std::wstring(), this, false),
language_list_(LanguageLibrary::Get()->GetActiveLanguages()),
model_(NULL),
// Be aware that the constructor of |language_menu_| calls GetItemCount()
// in this class. Therefore, GetItemCount() have to return 0 when
// |model_| is NULL.
ALLOW_THIS_IN_INITIALIZER_LIST(language_menu_(this)),
host_(host) {
DCHECK(language_list_.get() && !language_list_->empty());
// Update the model
RebuildModel();
// Grab the real estate.
UpdateIcon(kSpacer);
// Display the default XKB name (usually "US").
const std::string name = FormatInputLanguage(language_list_->at(0), false);
UpdateIcon(UTF8ToWide(name));
LanguageLibrary::Get()->AddObserver(this);
}
LanguageMenuButton::~LanguageMenuButton() {
LanguageLibrary::Get()->RemoveObserver(this);
}
////////////////////////////////////////////////////////////////////////////////
// LanguageMenuButton, menus::MenuModel implementation:
int LanguageMenuButton::GetCommandIdAt(int index) const {
return 0; // dummy
}
bool LanguageMenuButton::IsLabelDynamicAt(int index) const {
// Menu content for the language button could change time by time.
return true;
}
bool LanguageMenuButton::GetAcceleratorAt(
int index, menus::Accelerator* accelerator) const {
// Views for Chromium OS does not support accelerators yet.
return false;
}
bool LanguageMenuButton::IsItemCheckedAt(int index) const {
DCHECK_GE(index, 0);
DCHECK(language_list_.get());
if (IndexIsInLanguageList(index)) {
const InputLanguage& language = language_list_->at(index);
return language == LanguageLibrary::Get()->current_language();
}
if (GetPropertyIndex(index, &index)) {
const ImePropertyList& property_list
= LanguageLibrary::Get()->current_ime_properties();
return property_list.at(index).is_selection_item_checked;
}
// Separator(s) or the "Configure IME" button.
return false;
}
int LanguageMenuButton::GetGroupIdAt(int index) const {
DCHECK_GE(index, 0);
if (IndexIsInLanguageList(index)) {
return kRadioGroupLanguage;
}
if (GetPropertyIndex(index, &index)) {
const ImePropertyList& property_list
= LanguageLibrary::Get()->current_ime_properties();
return property_list.at(index).selection_item_id;
}
return kRadioGroupNone;
}
bool LanguageMenuButton::HasIcons() const {
// We don't support IME nor keyboard icons on Chrome OS.
return false;
}
bool LanguageMenuButton::GetIconAt(int index, SkBitmap* icon) const {
return false;
}
bool LanguageMenuButton::IsEnabledAt(int index) const {
// Just return true so all IMEs, XKB layouts, and IME properties could be
// clicked.
return true;
}
menus::MenuModel* LanguageMenuButton::GetSubmenuModelAt(int index) const {
// We don't use nested menus.
return NULL;
}
void LanguageMenuButton::HighlightChangedTo(int index) {
// Views for Chromium OS does not support this interface yet.
}
void LanguageMenuButton::MenuWillShow() {
// Views for Chromium OS does not support this interface yet.
}
int LanguageMenuButton::GetItemCount() const {
if (!model_.get()) {
// Model is not constructed yet. This means that LanguageMenuButton is
// being constructed. Return zero.
return 0;
}
return model_->GetItemCount();
}
menus::MenuModel::ItemType LanguageMenuButton::GetTypeAt(int index) const {
DCHECK_GE(index, 0);
if (IndexPointsToConfigureImeMenuItem(index)) {
return menus::MenuModel::TYPE_COMMAND; // "Configure IME"
}
if (IndexIsInLanguageList(index)) {
return menus::MenuModel::TYPE_RADIO;
}
if (GetPropertyIndex(index, &index)) {
const ImePropertyList& property_list
= LanguageLibrary::Get()->current_ime_properties();
if (property_list.at(index).is_selection_item) {
return menus::MenuModel::TYPE_RADIO;
}
return menus::MenuModel::TYPE_COMMAND;
}
return menus::MenuModel::TYPE_SEPARATOR;
}
string16 LanguageMenuButton::GetLabelAt(int index) const {
DCHECK_GE(index, 0);
DCHECK(language_list_.get());
if (IndexPointsToConfigureImeMenuItem(index)) {
return l10n_util::GetStringUTF16(IDS_STATUSBAR_IME_CONFIGURE);
}
std::string name;
if (IndexIsInLanguageList(index)) {
name = FormatInputLanguage(language_list_->at(index), true);
} else if (GetPropertyIndex(index, &index)) {
const ImePropertyList& property_list
= LanguageLibrary::Get()->current_ime_properties();
name = property_list.at(index).label;
}
return UTF8ToUTF16(name);
}
void LanguageMenuButton::ActivatedAt(int index) {
DCHECK_GE(index, 0);
DCHECK(language_list_.get());
if (IndexPointsToConfigureImeMenuItem(index)) {
host_->OpenButtonOptions(this);
return;
}
if (IndexIsInLanguageList(index)) {
// Inter-IME switching or IME-XKB switching.
const InputLanguage& language = language_list_->at(index);
LanguageLibrary::Get()->ChangeLanguage(language.category, language.id);
return;
}
if (GetPropertyIndex(index, &index)) {
// Intra-IME switching (e.g. Japanese-Hiragana to Japanese-Katakana).
const ImePropertyList& property_list
= LanguageLibrary::Get()->current_ime_properties();
const std::string key = property_list.at(index).key;
if (property_list.at(index).is_selection_item) {
// Radio button is clicked.
const int id = property_list.at(index).selection_item_id;
// First, deactivate all other properties in the same radio group.
for (int i = 0; i < static_cast<int>(property_list.size()); ++i) {
if (i != index && id == property_list.at(i).selection_item_id) {
LanguageLibrary::Get()->DeactivateImeProperty(
property_list.at(i).key);
}
}
// Then, activate the property clicked.
LanguageLibrary::Get()->ActivateImeProperty(key);
} else {
// Command button like "Switch to half punctuation mode" is clicked.
// We can always use "Deactivate" for command buttons.
LanguageLibrary::Get()->DeactivateImeProperty(key);
}
return;
}
// Separators are not clickable.
NOTREACHED();
}
////////////////////////////////////////////////////////////////////////////////
// LanguageMenuButton, views::ViewMenuDelegate implementation:
void LanguageMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
language_list_.reset(LanguageLibrary::Get()->GetActiveLanguages());
RebuildModel();
language_menu_.Rebuild();
language_menu_.UpdateStates();
language_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT);
}
////////////////////////////////////////////////////////////////////////////////
// LanguageLibrary::Observer implementation:
void LanguageMenuButton::LanguageChanged(LanguageLibrary* obj) {
const std::string name = FormatInputLanguage(obj->current_language(), false);
UpdateIcon(UTF8ToWide(name));
// This is necessary to remove IME properties when the current language is
// switched to XKB.
RebuildModel();
}
void LanguageMenuButton::ImePropertiesChanged(LanguageLibrary* obj) {
RebuildModel();
}
void LanguageMenuButton::UpdateIcon(const std::wstring& name) {
set_border(NULL);
SetFont(ResourceBundle::GetSharedInstance().GetFont(
ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD));
SetEnabledColor(SK_ColorWHITE);
SetShowHighlighted(false);
SetText(name);
set_alignment(TextButton::ALIGN_RIGHT);
SchedulePaint();
}
void LanguageMenuButton::RebuildModel() {
model_.reset(new menus::SimpleMenuModel(NULL));
string16 dummy_label = UTF8ToUTF16("");
// Indicates if separator's needed before each section.
bool need_separator = false;
if (!language_list_->empty()) {
// We "abuse" the command_id and group_id arguments of AddRadioItem method.
// A COMMAND_ID_XXX enum value is passed as command_id, and array index of
// |language_list_| or |property_list| is passed as group_id.
for (size_t i = 0; i < language_list_->size(); ++i) {
model_->AddRadioItem(COMMAND_ID_LANGUAGES, dummy_label, i);
}
need_separator = true;
}
const ImePropertyList& property_list
= LanguageLibrary::Get()->current_ime_properties();
const InputLanguage& current_language
= LanguageLibrary::Get()->current_language();
if ((!property_list.empty()) &&
(current_language.category == chromeos::LANGUAGE_CATEGORY_IME)) {
if (need_separator)
model_->AddSeparator();
for (size_t i = 0; i < property_list.size(); ++i) {
model_->AddRadioItem(COMMAND_ID_IME_PROPERTIES, dummy_label, i);
}
need_separator = true;
}
if (host_->ShouldOpenButtonOptions(this)) {
// Note: We use AddSeparator() for separators, and AddRadioItem() for all
// other items even if an item is not actually a radio item.
if (need_separator)
model_->AddSeparator();
model_->AddRadioItem(COMMAND_ID_CONFIGURE_IME, dummy_label, 0 /* dummy */);
}
}
bool LanguageMenuButton::IndexIsInLanguageList(int index) const {
DCHECK_GE(index, 0);
DCHECK(model_.get());
return ((model_->GetTypeAt(index) == menus::MenuModel::TYPE_RADIO) &&
(model_->GetCommandIdAt(index) == COMMAND_ID_LANGUAGES));
}
bool LanguageMenuButton::GetPropertyIndex(
int index, int* property_index) const {
DCHECK_GE(index, 0);
DCHECK(property_index);
DCHECK(model_.get());
if ((model_->GetTypeAt(index) == menus::MenuModel::TYPE_RADIO) &&
(model_->GetCommandIdAt(index) == COMMAND_ID_IME_PROPERTIES)) {
*property_index = model_->GetGroupIdAt(index);
return true;
}
return false;
}
bool LanguageMenuButton::IndexPointsToConfigureImeMenuItem(int index) const {
DCHECK_GE(index, 0);
DCHECK(model_.get());
return ((model_->GetTypeAt(index) == menus::MenuModel::TYPE_RADIO) &&
(model_->GetCommandIdAt(index) == COMMAND_ID_CONFIGURE_IME));
}
// TODO(yusukes): Register and handle hotkeys for IME and XKB switching?
} // namespace chromeos
|