input_method_menu.h revision 731df977c0511bca2206b5f333555b1205ff1f43
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#ifndef CHROME_BROWSER_CHROMEOS_STATUS_INPUT_METHOD_MENU_H_
6#define CHROME_BROWSER_CHROMEOS_STATUS_INPUT_METHOD_MENU_H_
7#pragma once
8
9#include "app/menus/simple_menu_model.h"
10#include "chrome/browser/chromeos/cros/input_method_library.h"
11#include "chrome/browser/prefs/pref_member.h"
12#include "chrome/common/notification_observer.h"
13#include "chrome/common/notification_registrar.h"
14#include "chrome/common/notification_service.h"
15#include "chrome/common/notification_type.h"
16#include "views/controls/menu/menu_2.h"
17#include "views/controls/menu/view_menu_delegate.h"
18
19class PrefService;
20class SkBitmap;
21
22namespace chromeos {
23
24// A class for the dropdown menu for switching input method and keyboard layout.
25// Since the class provides the views::ViewMenuDelegate interface, it's easy to
26// create a button widget (e.g. views::MenuButton, chromeos::StatusAreaButton)
27// which shows the dropdown menu on click.
28class InputMethodMenu : public views::ViewMenuDelegate,
29                        public menus::MenuModel,
30                        public InputMethodLibrary::Observer,
31                        public NotificationObserver {
32 public:
33  InputMethodMenu(PrefService* pref_service,
34                  bool is_browser_mode,
35                  bool is_screen_locker);
36  virtual ~InputMethodMenu();
37
38  // menus::MenuModel implementation.
39  virtual bool HasIcons() const;
40  virtual int GetItemCount() const;
41  virtual menus::MenuModel::ItemType GetTypeAt(int index) const;
42  virtual int GetCommandIdAt(int index) const;
43  virtual string16 GetLabelAt(int index) const;
44  virtual bool IsLabelDynamicAt(int index) const;
45  virtual bool GetAcceleratorAt(int index,
46                                menus::Accelerator* accelerator) const;
47  virtual bool IsItemCheckedAt(int index) const;
48  virtual int GetGroupIdAt(int index) const;
49  virtual bool GetIconAt(int index, SkBitmap* icon) const;
50  virtual menus::ButtonMenuItemModel* GetButtonMenuItemAt(int index) const;
51  virtual bool IsEnabledAt(int index) const;
52  virtual menus::MenuModel* GetSubmenuModelAt(int index) const;
53  virtual void HighlightChangedTo(int index);
54  virtual void ActivatedAt(int index);
55  virtual void MenuWillShow();
56
57  // views::ViewMenuDelegate implementation. Sub classes can override the method
58  // to adjust the position of the menu.
59  virtual void RunMenu(views::View* unused_source,
60                       const gfx::Point& pt);
61
62  // InputMethodLibrary::Observer implementation.
63  virtual void InputMethodChanged(InputMethodLibrary* obj);
64  virtual void ImePropertiesChanged(InputMethodLibrary* obj);
65  virtual void ActiveInputMethodsChanged(InputMethodLibrary* obj);
66
67  // NotificationObserver implementation.
68  virtual void Observe(NotificationType type,
69                       const NotificationSource& source,
70                       const NotificationDetails& details);
71
72  // Sets the minimum width of the dropdown menu.
73  void SetMinimumWidth(int width);
74
75  // Registers input method preferences for the login screen.
76  static void RegisterPrefs(PrefService* local_state);
77
78  // Returns a string for the indicator on top right corner of the Chrome
79  // window. The method is public for unit tests.
80  static std::wstring GetTextForIndicator(
81      const InputMethodDescriptor& input_method);
82
83  // Returns a string for the drop-down menu and the tooltip for the indicator.
84  // The method is public for unit tests.
85  static std::wstring GetTextForMenu(const InputMethodDescriptor& input_method);
86
87 protected:
88  // Parses |input_method| and then calls UpdateUI().
89  void UpdateUIFromInputMethod(const InputMethodDescriptor& input_method);
90
91  // Rebuilds model and menu2 objects in preparetion to open the menu.
92  void PrepareForMenuOpen();
93
94  // Returns menu2 object for language menu.
95  views::Menu2& language_menu() {
96    return language_menu_;
97  }
98
99 private:
100  // Updates UI of a container of the menu (e.g. the "US" menu button in the
101  // status area). Sub classes have to implement the interface for their own UI.
102  virtual void UpdateUI(
103      const std::wstring& name, const std::wstring& tooltip) = 0;
104
105  // Sub classes have to implement the interface. This interface should return
106  // true if the dropdown menu should show an item like "Customize languages
107  // and input..." DOMUI.
108  virtual bool ShouldSupportConfigUI() = 0;
109
110  // Sub classes have to implement the interface which opens an UI for
111  // customizing languages and input.
112  virtual void OpenConfigUI() = 0;
113
114  // Rebuilds |model_|. This function should be called whenever
115  // |input_method_descriptors_| is updated, or ImePropertiesChanged() is
116  // called.
117  void RebuildModel();
118
119  // Returns true if the zero-origin |index| points to one of the input methods.
120  bool IndexIsInInputMethodList(int index) const;
121
122  // Returns true if the zero-origin |index| points to one of the IME
123  // properties. When returning true, |property_index| is updated so that
124  // property_list.at(property_index) points to the menu item.
125  bool GetPropertyIndex(int index, int* property_index) const;
126
127  // Returns true if the zero-origin |index| points to the "Configure IME" menu
128  // item.
129  bool IndexPointsToConfigureImeMenuItem(int index) const;
130
131  // The current input method list.
132  scoped_ptr<InputMethodDescriptors> input_method_descriptors_;
133
134  // Objects for reading/writing the Chrome prefs.
135  StringPrefMember previous_input_method_pref_;
136  StringPrefMember current_input_method_pref_;
137
138  // We borrow menus::SimpleMenuModel implementation to maintain the current
139  // content of the pop-up menu. The menus::MenuModel is implemented using this
140  // |model_|.
141  scoped_ptr<menus::SimpleMenuModel> model_;
142
143  // The language menu which pops up when the button in status area is clicked.
144  views::Menu2 language_menu_;
145  int minimum_language_menu_width_;
146
147  PrefService* pref_service_;
148  NotificationRegistrar registrar_;
149  bool logged_in_;
150  const bool is_browser_mode_;
151  const bool is_screen_locker_mode_;
152
153  DISALLOW_COPY_AND_ASSIGN(InputMethodMenu);
154};
155
156}  // namespace chromeos
157
158#endif  // CHROME_BROWSER_CHROMEOS_STATUS_INPUT_METHOD_MENU_H_
159