OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 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 |
5 #ifndef UI_BASE_MODELS_ACCELERATOR_H_ | 11 #ifndef UI_BASE_MODELS_ACCELERATOR_H_ |
6 #define UI_BASE_MODELS_ACCELERATOR_H_ | 12 #define UI_BASE_MODELS_ACCELERATOR_H_ |
7 #pragma once | 13 #pragma once |
8 | 14 |
| 15 #include "base/string16.h" |
9 #include "ui/base/keycodes/keyboard_codes.h" | 16 #include "ui/base/keycodes/keyboard_codes.h" |
| 17 #include "ui/base/events.h" |
10 #include "ui/base/ui_export.h" | 18 #include "ui/base/ui_export.h" |
11 | 19 |
12 namespace ui { | 20 namespace ui { |
13 | 21 |
14 // This is a cross-platform base class for accelerator keys used in menus. It is | 22 // This is a cross-platform base class for accelerator keys used in menus. It is |
15 // meant to be subclassed for concrete toolkit implementations. | 23 // meant to be subclassed for concrete toolkit implementations. |
16 class UI_EXPORT Accelerator { | 24 class UI_EXPORT Accelerator { |
17 public: | 25 public: |
18 Accelerator() : key_code_(ui::VKEY_UNKNOWN), modifiers_(0) {} | 26 Accelerator() : key_code_(ui::VKEY_UNKNOWN), modifiers_(0) {} |
19 | 27 |
20 Accelerator(ui::KeyboardCode keycode, int modifiers) | 28 Accelerator(ui::KeyboardCode keycode, int modifiers) |
21 : key_code_(keycode), | 29 : key_code_(keycode), |
22 modifiers_(modifiers) {} | 30 modifiers_(modifiers) {} |
23 | 31 |
24 Accelerator(const Accelerator& accelerator) { | 32 Accelerator(const Accelerator& accelerator) { |
25 key_code_ = accelerator.key_code_; | 33 key_code_ = accelerator.key_code_; |
26 modifiers_ = accelerator.modifiers_; | 34 modifiers_ = accelerator.modifiers_; |
27 } | 35 } |
28 | 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 |
29 virtual ~Accelerator() {} | 49 virtual ~Accelerator() {} |
30 | 50 |
31 Accelerator& operator=(const Accelerator& accelerator) { | 51 Accelerator& operator=(const Accelerator& accelerator) { |
32 if (this != &accelerator) { | 52 if (this != &accelerator) { |
33 key_code_ = accelerator.key_code_; | 53 key_code_ = accelerator.key_code_; |
34 modifiers_ = accelerator.modifiers_; | 54 modifiers_ = accelerator.modifiers_; |
35 } | 55 } |
36 return *this; | 56 return *this; |
37 } | 57 } |
38 | 58 |
(...skipping 10 matching lines...) Expand all Loading... |
49 } | 69 } |
50 | 70 |
51 bool operator !=(const Accelerator& rhs) const { | 71 bool operator !=(const Accelerator& rhs) const { |
52 return !(*this == rhs); | 72 return !(*this == rhs); |
53 } | 73 } |
54 | 74 |
55 ui::KeyboardCode key_code() const { return key_code_; } | 75 ui::KeyboardCode key_code() const { return key_code_; } |
56 | 76 |
57 int modifiers() const { return modifiers_; } | 77 int modifiers() const { return modifiers_; } |
58 | 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 |
59 protected: | 94 protected: |
60 // The keycode (VK_...). | 95 // The keycode (VK_...). |
61 ui::KeyboardCode key_code_; | 96 ui::KeyboardCode key_code_; |
62 | 97 |
63 // The state of the Shift/Ctrl/Alt keys (platform-dependent). | 98 // The state of the Shift/Ctrl/Alt keys (platform-dependent). |
64 int modifiers_; | 99 int modifiers_; |
65 }; | 100 }; |
66 | 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 |
67 // Since acclerator code is one of the few things that can't be cross platform | 113 // Since acclerator code is one of the few things that can't be cross platform |
68 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from | 114 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from |
69 // the menu delegates. | 115 // the menu delegates. |
70 class AcceleratorProvider { | 116 class AcceleratorProvider { |
71 public: | 117 public: |
72 // Gets the accelerator for the specified command id. Returns true if the | 118 // Gets the accelerator for the specified command id. Returns true if the |
73 // command id has a valid accelerator, false otherwise. | 119 // command id has a valid accelerator, false otherwise. |
74 virtual bool GetAcceleratorForCommandId(int command_id, | 120 virtual bool GetAcceleratorForCommandId(int command_id, |
75 ui::Accelerator* accelerator) = 0; | 121 ui::Accelerator* accelerator) = 0; |
76 | 122 |
77 protected: | 123 protected: |
78 virtual ~AcceleratorProvider() {} | 124 virtual ~AcceleratorProvider() {} |
79 }; | 125 }; |
80 | 126 |
81 } // namespace ui | 127 } // namespace ui |
82 | 128 |
83 #endif // UI_BASE_MODELS_ACCELERATOR_H_ | 129 #endif // UI_BASE_MODELS_ACCELERATOR_H_ |
OLD | NEW |