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/login/keyboard_switch_menu.h"
6
7#include "base/i18n/rtl.h"
8#include "base/utf_string_conversions.h"
9#include "chrome/browser/chromeos/cros/cros_library.h"
10#include "chrome/browser/chromeos/input_method/input_method_util.h"
11#include "chrome/browser/chromeos/status/status_area_host.h"
12#include "grit/generated_resources.h"
13#include "ui/base/l10n/l10n_util.h"
14#include "views/controls/button/menu_button.h"
15#include "views/widget/widget_gtk.h"
16
17namespace chromeos {
18
19KeyboardSwitchMenu::KeyboardSwitchMenu()
20    : InputMethodMenu(NULL /* pref_service */,
21                      StatusAreaHost::kLoginMode,
22                      true /* for_out_of_box_experience_dialog */) {
23}
24
25////////////////////////////////////////////////////////////////////////////////
26// InputMethodMenu::InputMethodMenuHost implementation.
27void KeyboardSwitchMenu::UpdateUI(const std::string& input_method_id,
28                                  const std::wstring& name,
29                                  const std::wstring& tooltip,
30                                  size_t num_active_input_methods) {
31  // Update all view hierarchies so that the new input method name is shown in
32  // the menu button.
33  views::Widget::NotifyLocaleChanged();
34}
35
36////////////////////////////////////////////////////////////////////////////////
37// views::ViewMenuDelegate implementation.
38void KeyboardSwitchMenu::RunMenu(views::View* source, const gfx::Point& pt) {
39  PrepareForMenuOpen();
40  gfx::Point new_pt(pt);
41  views::MenuButton* button = static_cast<views::MenuButton*>(source);
42  // Keyboard switch menu is aligned on left by default.
43  int reverse_offset = button->width() + button->menu_offset().x() * 2;
44  if (base::i18n::IsRTL()) {
45    new_pt.set_x(pt.x() + reverse_offset);
46  } else {
47    new_pt.set_x(pt.x() - reverse_offset);
48  }
49  input_method_menu().RunMenuAt(new_pt, views::Menu2::ALIGN_TOPLEFT);
50}
51
52string16 KeyboardSwitchMenu::GetCurrentKeyboardName() const {
53  const int count = GetItemCount();
54  for (int i = 0; i < count; ++i) {
55    if (IsItemCheckedAt(i))
56      return GetLabelAt(i);
57  }
58  VLOG(1) << "The input method menu is not ready yet. Show the display "
59          << "name of the current input method";
60  InputMethodLibrary* library = CrosLibrary::Get()->GetInputMethodLibrary();
61  return UTF8ToUTF16(input_method::GetInputMethodDisplayNameFromId(
62      library->current_input_method().id));
63}
64
65}  // namespace chromeos
66