Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / base / accelerators / accelerator.cc
1 // Copyright (c) 2012 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 "ui/base/accelerators/accelerator.h"
6
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #elif defined(TOOLKIT_GTK)
10 #include <gdk/gdk.h>
11 #endif
12
13 #include "base/i18n/rtl.h"
14 #include "base/logging.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "grit/ui_strings.h"
18 #include "ui/base/l10n/l10n_util.h"
19
20 #if !defined(OS_WIN) && (defined(USE_AURA) || defined(OS_MACOSX))
21 #include "ui/events/keycodes/keyboard_code_conversion.h"
22 #endif
23
24 namespace ui {
25
26 Accelerator::Accelerator()
27     : key_code_(ui::VKEY_UNKNOWN),
28       type_(ui::ET_KEY_PRESSED),
29       modifiers_(0) {
30 }
31
32 Accelerator::Accelerator(KeyboardCode keycode, int modifiers)
33     : key_code_(keycode),
34       type_(ui::ET_KEY_PRESSED),
35       modifiers_(modifiers) {
36 }
37
38 Accelerator::Accelerator(const Accelerator& accelerator) {
39   key_code_ = accelerator.key_code_;
40   type_ = accelerator.type_;
41   modifiers_ = accelerator.modifiers_;
42   if (accelerator.platform_accelerator_.get())
43     platform_accelerator_ = accelerator.platform_accelerator_->CreateCopy();
44 }
45
46 Accelerator::~Accelerator() {
47 }
48
49 Accelerator& Accelerator::operator=(const Accelerator& accelerator) {
50   if (this != &accelerator) {
51     key_code_ = accelerator.key_code_;
52     type_ = accelerator.type_;
53     modifiers_ = accelerator.modifiers_;
54     if (accelerator.platform_accelerator_.get())
55       platform_accelerator_ = accelerator.platform_accelerator_->CreateCopy();
56     else
57       platform_accelerator_.reset();
58   }
59   return *this;
60 }
61
62 bool Accelerator::operator <(const Accelerator& rhs) const {
63   if (key_code_ != rhs.key_code_)
64     return key_code_ < rhs.key_code_;
65   if (type_ != rhs.type_)
66     return type_ < rhs.type_;
67   return modifiers_ < rhs.modifiers_;
68 }
69
70 bool Accelerator::operator ==(const Accelerator& rhs) const {
71   if ((key_code_ == rhs.key_code_) && (type_ == rhs.type_) &&
72       (modifiers_ == rhs.modifiers_))
73     return true;
74
75   bool platform_equal =
76       platform_accelerator_.get() && rhs.platform_accelerator_.get() &&
77       platform_accelerator_.get() == rhs.platform_accelerator_.get();
78
79   return platform_equal;
80 }
81
82 bool Accelerator::operator !=(const Accelerator& rhs) const {
83   return !(*this == rhs);
84 }
85
86 bool Accelerator::IsShiftDown() const {
87   return (modifiers_ & EF_SHIFT_DOWN) != 0;
88 }
89
90 bool Accelerator::IsCtrlDown() const {
91   return (modifiers_ & EF_CONTROL_DOWN) != 0;
92 }
93
94 bool Accelerator::IsAltDown() const {
95   return (modifiers_ & EF_ALT_DOWN) != 0;
96 }
97
98 bool Accelerator::IsCmdDown() const {
99   return (modifiers_ & EF_COMMAND_DOWN) != 0;
100 }
101
102 base::string16 Accelerator::GetShortcutText() const {
103   int string_id = 0;
104   switch (key_code_) {
105     case ui::VKEY_TAB:
106       string_id = IDS_APP_TAB_KEY;
107       break;
108     case ui::VKEY_RETURN:
109       string_id = IDS_APP_ENTER_KEY;
110       break;
111     case ui::VKEY_ESCAPE:
112       string_id = IDS_APP_ESC_KEY;
113       break;
114     case ui::VKEY_PRIOR:
115       string_id = IDS_APP_PAGEUP_KEY;
116       break;
117     case ui::VKEY_NEXT:
118       string_id = IDS_APP_PAGEDOWN_KEY;
119       break;
120     case ui::VKEY_END:
121       string_id = IDS_APP_END_KEY;
122       break;
123     case ui::VKEY_HOME:
124       string_id = IDS_APP_HOME_KEY;
125       break;
126     case ui::VKEY_INSERT:
127       string_id = IDS_APP_INSERT_KEY;
128       break;
129     case ui::VKEY_DELETE:
130       string_id = IDS_APP_DELETE_KEY;
131       break;
132     case ui::VKEY_LEFT:
133       string_id = IDS_APP_LEFT_ARROW_KEY;
134       break;
135     case ui::VKEY_RIGHT:
136       string_id = IDS_APP_RIGHT_ARROW_KEY;
137       break;
138     case ui::VKEY_UP:
139       string_id = IDS_APP_UP_ARROW_KEY;
140       break;
141     case ui::VKEY_DOWN:
142       string_id = IDS_APP_DOWN_ARROW_KEY;
143       break;
144     case ui::VKEY_BACK:
145       string_id = IDS_APP_BACKSPACE_KEY;
146       break;
147     case ui::VKEY_F1:
148       string_id = IDS_APP_F1_KEY;
149       break;
150     case ui::VKEY_F11:
151       string_id = IDS_APP_F11_KEY;
152       break;
153     case ui::VKEY_OEM_COMMA:
154       string_id = IDS_APP_COMMA_KEY;
155       break;
156     case ui::VKEY_OEM_PERIOD:
157       string_id = IDS_APP_PERIOD_KEY;
158       break;
159     case ui::VKEY_MEDIA_NEXT_TRACK:
160       string_id = IDS_APP_MEDIA_NEXT_TRACK_KEY;
161       break;
162     case ui::VKEY_MEDIA_PLAY_PAUSE:
163       string_id = IDS_APP_MEDIA_PLAY_PAUSE_KEY;
164       break;
165     case ui::VKEY_MEDIA_PREV_TRACK:
166       string_id = IDS_APP_MEDIA_PREV_TRACK_KEY;
167       break;
168     case ui::VKEY_MEDIA_STOP:
169       string_id = IDS_APP_MEDIA_STOP_KEY;
170       break;
171     default:
172       break;
173   }
174
175   base::string16 shortcut;
176   if (!string_id) {
177 #if defined(OS_WIN)
178     // Our fallback is to try translate the key code to a regular character
179     // unless it is one of digits (VK_0 to VK_9). Some keyboard
180     // layouts have characters other than digits assigned in
181     // an unshifted mode (e.g. French AZERY layout has 'a with grave
182     // accent' for '0'). For display in the menu (e.g. Ctrl-0 for the
183     // default zoom level), we leave VK_[0-9] alone without translation.
184     wchar_t key;
185     if (key_code_ >= '0' && key_code_ <= '9')
186       key = key_code_;
187     else
188       key = LOWORD(::MapVirtualKeyW(key_code_, MAPVK_VK_TO_CHAR));
189     shortcut += key;
190 #elif defined(USE_AURA) || defined(OS_MACOSX)
191     const uint16 c = GetCharacterFromKeyCode(key_code_, false);
192     if (c != 0)
193       shortcut +=
194           static_cast<base::string16::value_type>(base::ToUpperASCII(c));
195 #elif defined(TOOLKIT_GTK)
196     const gchar* name = NULL;
197     switch (key_code_) {
198       case ui::VKEY_OEM_2:
199         name = static_cast<const gchar*>("/");
200         break;
201       default:
202         name = gdk_keyval_name(gdk_keyval_to_lower(key_code_));
203         break;
204     }
205     if (name) {
206       if (name[0] != 0 && name[1] == 0)
207         shortcut +=
208             static_cast<base::string16::value_type>(g_ascii_toupper(name[0]));
209       else
210         shortcut += base::UTF8ToUTF16(name);
211     }
212 #endif
213   } else {
214     shortcut = l10n_util::GetStringUTF16(string_id);
215   }
216
217   // Checking whether the character used for the accelerator is alphanumeric.
218   // If it is not, then we need to adjust the string later on if the locale is
219   // right-to-left. See below for more information of why such adjustment is
220   // required.
221   base::string16 shortcut_rtl;
222   bool adjust_shortcut_for_rtl = false;
223   if (base::i18n::IsRTL() && shortcut.length() == 1 &&
224       !IsAsciiAlpha(shortcut[0]) && !IsAsciiDigit(shortcut[0])) {
225     adjust_shortcut_for_rtl = true;
226     shortcut_rtl.assign(shortcut);
227   }
228
229   if (IsShiftDown())
230     shortcut = l10n_util::GetStringFUTF16(IDS_APP_SHIFT_MODIFIER, shortcut);
231
232   // Note that we use 'else-if' in order to avoid using Ctrl+Alt as a shortcut.
233   // See http://blogs.msdn.com/oldnewthing/archive/2004/03/29/101121.aspx for
234   // more information.
235   if (IsCtrlDown())
236     shortcut = l10n_util::GetStringFUTF16(IDS_APP_CONTROL_MODIFIER, shortcut);
237   else if (IsAltDown())
238     shortcut = l10n_util::GetStringFUTF16(IDS_APP_ALT_MODIFIER, shortcut);
239
240   if (IsCmdDown())
241     shortcut = l10n_util::GetStringFUTF16(IDS_APP_COMMAND_MODIFIER, shortcut);
242
243   // For some reason, menus in Windows ignore standard Unicode directionality
244   // marks (such as LRE, PDF, etc.). On RTL locales, we use RTL menus and
245   // therefore any text we draw for the menu items is drawn in an RTL context.
246   // Thus, the text "Ctrl++" (which we currently use for the Zoom In option)
247   // appears as "++Ctrl" in RTL because the Unicode BiDi algorithm puts
248   // punctuations on the left when the context is right-to-left. Shortcuts that
249   // do not end with a punctuation mark (such as "Ctrl+H" do not have this
250   // problem).
251   //
252   // The only way to solve this problem is to adjust the string if the locale
253   // is RTL so that it is drawn correctly in an RTL context. Instead of
254   // returning "Ctrl++" in the above example, we return "++Ctrl". This will
255   // cause the text to appear as "Ctrl++" when Windows draws the string in an
256   // RTL context because the punctuation no longer appears at the end of the
257   // string.
258   //
259   // TODO(idana) bug# 1232732: this hack can be avoided if instead of using
260   // views::Menu we use views::MenuItemView because the latter is a View
261   // subclass and therefore it supports marking text as RTL or LTR using
262   // standard Unicode directionality marks.
263   if (adjust_shortcut_for_rtl) {
264     int key_length = static_cast<int>(shortcut_rtl.length());
265     DCHECK_GT(key_length, 0);
266     shortcut_rtl.append(base::ASCIIToUTF16("+"));
267
268     // Subtracting the size of the shortcut key and 1 for the '+' sign.
269     shortcut_rtl.append(shortcut, 0, shortcut.length() - key_length - 1);
270     shortcut.swap(shortcut_rtl);
271   }
272
273   return shortcut;
274 }
275
276 }  // namespace ui