| OLD | NEW |
| (Empty) |
| 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 // This class describe a keyboard accelerator (or keyboard shortcut). | |
| 6 // Keyboard accelerators are registered with the FocusManager. | |
| 7 // It has a copy constructor and assignment operator so that it can be copied. | |
| 8 // It also defines the < operator so that it can be used as a key in a std::map. | |
| 9 // | |
| 10 | |
| 11 #ifndef UI_BASE_MODELS_ACCELERATOR_H_ | |
| 12 #define UI_BASE_MODELS_ACCELERATOR_H_ | |
| 13 #pragma once | |
| 14 | |
| 15 #include "base/string16.h" | |
| 16 #include "ui/base/keycodes/keyboard_codes.h" | |
| 17 #include "ui/base/events.h" | |
| 18 #include "ui/base/ui_export.h" | |
| 19 | |
| 20 namespace ui { | |
| 21 | |
| 22 // This is a cross-platform base class for accelerator keys used in menus. It is | |
| 23 // meant to be subclassed for concrete toolkit implementations. | |
| 24 class UI_EXPORT Accelerator { | |
| 25 public: | |
| 26 Accelerator() : key_code_(ui::VKEY_UNKNOWN), modifiers_(0) {} | |
| 27 | |
| 28 Accelerator(ui::KeyboardCode keycode, int modifiers) | |
| 29 : key_code_(keycode), | |
| 30 modifiers_(modifiers) {} | |
| 31 | |
| 32 Accelerator(const Accelerator& accelerator) { | |
| 33 key_code_ = accelerator.key_code_; | |
| 34 modifiers_ = accelerator.modifiers_; | |
| 35 } | |
| 36 | |
| 37 Accelerator(ui::KeyboardCode keycode, | |
| 38 bool shift_pressed, bool ctrl_pressed, bool alt_pressed) | |
| 39 : key_code_(keycode), | |
| 40 modifiers_(0) { | |
| 41 if (shift_pressed) | |
| 42 modifiers_ |= ui::EF_SHIFT_DOWN; | |
| 43 if (ctrl_pressed) | |
| 44 modifiers_ |= ui::EF_CONTROL_DOWN; | |
| 45 if (alt_pressed) | |
| 46 modifiers_ |= ui::EF_ALT_DOWN; | |
| 47 } | |
| 48 | |
| 49 virtual ~Accelerator() {} | |
| 50 | |
| 51 Accelerator& operator=(const Accelerator& accelerator) { | |
| 52 if (this != &accelerator) { | |
| 53 key_code_ = accelerator.key_code_; | |
| 54 modifiers_ = accelerator.modifiers_; | |
| 55 } | |
| 56 return *this; | |
| 57 } | |
| 58 | |
| 59 // We define the < operator so that the KeyboardShortcut can be used as a key | |
| 60 // in a std::map. | |
| 61 bool operator <(const Accelerator& rhs) const { | |
| 62 if (key_code_ != rhs.key_code_) | |
| 63 return key_code_ < rhs.key_code_; | |
| 64 return modifiers_ < rhs.modifiers_; | |
| 65 } | |
| 66 | |
| 67 bool operator ==(const Accelerator& rhs) const { | |
| 68 return (key_code_ == rhs.key_code_) && (modifiers_ == rhs.modifiers_); | |
| 69 } | |
| 70 | |
| 71 bool operator !=(const Accelerator& rhs) const { | |
| 72 return !(*this == rhs); | |
| 73 } | |
| 74 | |
| 75 ui::KeyboardCode key_code() const { return key_code_; } | |
| 76 | |
| 77 int modifiers() const { return modifiers_; } | |
| 78 | |
| 79 bool IsShiftDown() const { | |
| 80 return (modifiers_ & ui::EF_SHIFT_DOWN) == ui::EF_SHIFT_DOWN; | |
| 81 } | |
| 82 | |
| 83 bool IsCtrlDown() const { | |
| 84 return (modifiers_ & ui::EF_CONTROL_DOWN) == ui::EF_CONTROL_DOWN; | |
| 85 } | |
| 86 | |
| 87 bool IsAltDown() const { | |
| 88 return (modifiers_ & ui::EF_ALT_DOWN) == ui::EF_ALT_DOWN; | |
| 89 } | |
| 90 | |
| 91 // Returns a string with the localized shortcut if any. | |
| 92 string16 GetShortcutText() const; | |
| 93 | |
| 94 protected: | |
| 95 // The keycode (VK_...). | |
| 96 ui::KeyboardCode key_code_; | |
| 97 | |
| 98 // The state of the Shift/Ctrl/Alt keys (platform-dependent). | |
| 99 int modifiers_; | |
| 100 }; | |
| 101 | |
| 102 // An interface that classes that want to register for keyboard accelerators | |
| 103 // should implement. | |
| 104 class UI_EXPORT AcceleratorTarget { | |
| 105 public: | |
| 106 // This method should return true if the accelerator was processed. | |
| 107 virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0; | |
| 108 | |
| 109 protected: | |
| 110 virtual ~AcceleratorTarget() {} | |
| 111 }; | |
| 112 | |
| 113 // Since acclerator code is one of the few things that can't be cross platform | |
| 114 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from | |
| 115 // the menu delegates. | |
| 116 class AcceleratorProvider { | |
| 117 public: | |
| 118 // Gets the accelerator for the specified command id. Returns true if the | |
| 119 // command id has a valid accelerator, false otherwise. | |
| 120 virtual bool GetAcceleratorForCommandId(int command_id, | |
| 121 ui::Accelerator* accelerator) = 0; | |
| 122 | |
| 123 protected: | |
| 124 virtual ~AcceleratorProvider() {} | |
| 125 }; | |
| 126 | |
| 127 } // namespace ui | |
| 128 | |
| 129 #endif // UI_BASE_MODELS_ACCELERATOR_H_ | |
| OLD | NEW |