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