This source file includes following definitions.
- modifiers_
- modifiers_
- IsShiftDown
- IsCtrlDown
- IsAltDown
- IsCmdDown
- GetShortcutText
#include "ui/base/accelerators/accelerator.h"
#if defined(OS_WIN)
#include <windows.h>
#elif defined(TOOLKIT_GTK)
#include <gdk/gdk.h>
#endif
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ui_strings.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(OS_WIN) && (defined(USE_AURA) || defined(OS_MACOSX))
#include "ui/events/keycodes/keyboard_code_conversion.h"
#endif
namespace ui {
Accelerator::Accelerator()
: key_code_(ui::VKEY_UNKNOWN),
type_(ui::ET_KEY_PRESSED),
modifiers_(0) {
}
Accelerator::Accelerator(KeyboardCode keycode, int modifiers)
: key_code_(keycode),
type_(ui::ET_KEY_PRESSED),
modifiers_(modifiers) {
}
Accelerator::Accelerator(const Accelerator& accelerator) {
key_code_ = accelerator.key_code_;
type_ = accelerator.type_;
modifiers_ = accelerator.modifiers_;
if (accelerator.platform_accelerator_.get())
platform_accelerator_ = accelerator.platform_accelerator_->CreateCopy();
}
Accelerator::~Accelerator() {
}
Accelerator& Accelerator::operator=(const Accelerator& accelerator) {
if (this != &accelerator) {
key_code_ = accelerator.key_code_;
type_ = accelerator.type_;
modifiers_ = accelerator.modifiers_;
if (accelerator.platform_accelerator_.get())
platform_accelerator_ = accelerator.platform_accelerator_->CreateCopy();
else
platform_accelerator_.reset();
}
return *this;
}
bool Accelerator::operator <(const Accelerator& rhs) const {
if (key_code_ != rhs.key_code_)
return key_code_ < rhs.key_code_;
if (type_ != rhs.type_)
return type_ < rhs.type_;
return modifiers_ < rhs.modifiers_;
}
bool Accelerator::operator ==(const Accelerator& rhs) const {
if ((key_code_ == rhs.key_code_) && (type_ == rhs.type_) &&
(modifiers_ == rhs.modifiers_))
return true;
bool platform_equal =
platform_accelerator_.get() && rhs.platform_accelerator_.get() &&
platform_accelerator_.get() == rhs.platform_accelerator_.get();
return platform_equal;
}
bool Accelerator::operator !=(const Accelerator& rhs) const {
return !(*this == rhs);
}
bool Accelerator::IsShiftDown() const {
return (modifiers_ & EF_SHIFT_DOWN) != 0;
}
bool Accelerator::IsCtrlDown() const {
return (modifiers_ & EF_CONTROL_DOWN) != 0;
}
bool Accelerator::IsAltDown() const {
return (modifiers_ & EF_ALT_DOWN) != 0;
}
bool Accelerator::IsCmdDown() const {
return (modifiers_ & EF_COMMAND_DOWN) != 0;
}
base::string16 Accelerator::GetShortcutText() const {
int string_id = 0;
switch (key_code_) {
case ui::VKEY_TAB:
string_id = IDS_APP_TAB_KEY;
break;
case ui::VKEY_RETURN:
string_id = IDS_APP_ENTER_KEY;
break;
case ui::VKEY_ESCAPE:
string_id = IDS_APP_ESC_KEY;
break;
case ui::VKEY_PRIOR:
string_id = IDS_APP_PAGEUP_KEY;
break;
case ui::VKEY_NEXT:
string_id = IDS_APP_PAGEDOWN_KEY;
break;
case ui::VKEY_END:
string_id = IDS_APP_END_KEY;
break;
case ui::VKEY_HOME:
string_id = IDS_APP_HOME_KEY;
break;
case ui::VKEY_INSERT:
string_id = IDS_APP_INSERT_KEY;
break;
case ui::VKEY_DELETE:
string_id = IDS_APP_DELETE_KEY;
break;
case ui::VKEY_LEFT:
string_id = IDS_APP_LEFT_ARROW_KEY;
break;
case ui::VKEY_RIGHT:
string_id = IDS_APP_RIGHT_ARROW_KEY;
break;
case ui::VKEY_UP:
string_id = IDS_APP_UP_ARROW_KEY;
break;
case ui::VKEY_DOWN:
string_id = IDS_APP_DOWN_ARROW_KEY;
break;
case ui::VKEY_BACK:
string_id = IDS_APP_BACKSPACE_KEY;
break;
case ui::VKEY_F1:
string_id = IDS_APP_F1_KEY;
break;
case ui::VKEY_F11:
string_id = IDS_APP_F11_KEY;
break;
case ui::VKEY_OEM_COMMA:
string_id = IDS_APP_COMMA_KEY;
break;
case ui::VKEY_OEM_PERIOD:
string_id = IDS_APP_PERIOD_KEY;
break;
case ui::VKEY_MEDIA_NEXT_TRACK:
string_id = IDS_APP_MEDIA_NEXT_TRACK_KEY;
break;
case ui::VKEY_MEDIA_PLAY_PAUSE:
string_id = IDS_APP_MEDIA_PLAY_PAUSE_KEY;
break;
case ui::VKEY_MEDIA_PREV_TRACK:
string_id = IDS_APP_MEDIA_PREV_TRACK_KEY;
break;
case ui::VKEY_MEDIA_STOP:
string_id = IDS_APP_MEDIA_STOP_KEY;
break;
default:
break;
}
base::string16 shortcut;
if (!string_id) {
#if defined(OS_WIN)
wchar_t key;
if (key_code_ >= '0' && key_code_ <= '9')
key = key_code_;
else
key = LOWORD(::MapVirtualKeyW(key_code_, MAPVK_VK_TO_CHAR));
shortcut += key;
#elif defined(USE_AURA) || defined(OS_MACOSX)
const uint16 c = GetCharacterFromKeyCode(key_code_, false);
if (c != 0)
shortcut +=
static_cast<base::string16::value_type>(base::ToUpperASCII(c));
#elif defined(TOOLKIT_GTK)
const gchar* name = NULL;
switch (key_code_) {
case ui::VKEY_OEM_2:
name = static_cast<const gchar*>("/");
break;
default:
name = gdk_keyval_name(gdk_keyval_to_lower(key_code_));
break;
}
if (name) {
if (name[0] != 0 && name[1] == 0)
shortcut +=
static_cast<base::string16::value_type>(g_ascii_toupper(name[0]));
else
shortcut += base::UTF8ToUTF16(name);
}
#endif
} else {
shortcut = l10n_util::GetStringUTF16(string_id);
}
base::string16 shortcut_rtl;
bool adjust_shortcut_for_rtl = false;
if (base::i18n::IsRTL() && shortcut.length() == 1 &&
!IsAsciiAlpha(shortcut[0]) && !IsAsciiDigit(shortcut[0])) {
adjust_shortcut_for_rtl = true;
shortcut_rtl.assign(shortcut);
}
if (IsShiftDown())
shortcut = l10n_util::GetStringFUTF16(IDS_APP_SHIFT_MODIFIER, shortcut);
if (IsCtrlDown())
shortcut = l10n_util::GetStringFUTF16(IDS_APP_CONTROL_MODIFIER, shortcut);
else if (IsAltDown())
shortcut = l10n_util::GetStringFUTF16(IDS_APP_ALT_MODIFIER, shortcut);
if (IsCmdDown())
shortcut = l10n_util::GetStringFUTF16(IDS_APP_COMMAND_MODIFIER, shortcut);
if (adjust_shortcut_for_rtl) {
int key_length = static_cast<int>(shortcut_rtl.length());
DCHECK_GT(key_length, 0);
shortcut_rtl.append(base::ASCIIToUTF16("+"));
shortcut_rtl.append(shortcut, 0, shortcut.length() - key_length - 1);
shortcut.swap(shortcut_rtl);
}
return shortcut;
}
}