OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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() | 26 Accelerator(); |
27 : key_code_(ui::VKEY_UNKNOWN), type_(ui::ET_KEY_PRESSED), modifiers_(0) {} | 27 Accelerator(ui::KeyboardCode keycode, int modifiers); |
| 28 Accelerator(const Accelerator& accelerator); |
| 29 ~Accelerator(); |
28 | 30 |
29 Accelerator(ui::KeyboardCode keycode, int modifiers) | 31 Accelerator& operator=(const Accelerator& accelerator); |
30 : key_code_(keycode), | |
31 type_(ui::ET_KEY_PRESSED), | |
32 modifiers_(modifiers) {} | |
33 | |
34 Accelerator(const Accelerator& accelerator) { | |
35 key_code_ = accelerator.key_code_; | |
36 type_ = accelerator.type_; | |
37 modifiers_ = accelerator.modifiers_; | |
38 } | |
39 | |
40 Accelerator(ui::KeyboardCode keycode, | |
41 bool shift_pressed, bool ctrl_pressed, bool alt_pressed) | |
42 : key_code_(keycode), | |
43 type_(ui::ET_KEY_PRESSED), | |
44 modifiers_(0) { | |
45 if (shift_pressed) | |
46 modifiers_ |= ui::EF_SHIFT_DOWN; | |
47 if (ctrl_pressed) | |
48 modifiers_ |= ui::EF_CONTROL_DOWN; | |
49 if (alt_pressed) | |
50 modifiers_ |= ui::EF_ALT_DOWN; | |
51 } | |
52 | |
53 virtual ~Accelerator() {} | |
54 | |
55 Accelerator& operator=(const Accelerator& accelerator) { | |
56 if (this != &accelerator) { | |
57 key_code_ = accelerator.key_code_; | |
58 type_ = accelerator.type_; | |
59 modifiers_ = accelerator.modifiers_; | |
60 } | |
61 return *this; | |
62 } | |
63 | 32 |
64 // We define the < operator so that the KeyboardShortcut can be used as a key | 33 // We define the < operator so that the KeyboardShortcut can be used as a key |
65 // in a std::map. | 34 // in a std::map. |
66 bool operator <(const Accelerator& rhs) const { | 35 bool operator <(const Accelerator& rhs) const; |
67 if (key_code_ != rhs.key_code_) | |
68 return key_code_ < rhs.key_code_; | |
69 if (type_ != rhs.type_) | |
70 return type_ < rhs.type_; | |
71 return modifiers_ < rhs.modifiers_; | |
72 } | |
73 | 36 |
74 bool operator ==(const Accelerator& rhs) const { | 37 bool operator ==(const Accelerator& rhs) const; |
75 return (key_code_ == rhs.key_code_) && | |
76 (type_ == rhs.type_) && (modifiers_ == rhs.modifiers_); | |
77 } | |
78 | 38 |
79 bool operator !=(const Accelerator& rhs) const { | 39 bool operator !=(const Accelerator& rhs) const; |
80 return !(*this == rhs); | |
81 } | |
82 | 40 |
83 ui::KeyboardCode key_code() const { return key_code_; } | 41 ui::KeyboardCode key_code() const { return key_code_; } |
84 | 42 |
85 ui::EventType type() const { return type_; } | |
86 | |
87 // Sets the event type if the accelerator should be processed on an event | 43 // Sets the event type if the accelerator should be processed on an event |
88 // other than ui::ET_KEY_PRESSED. | 44 // other than ui::ET_KEY_PRESSED. |
89 void set_type(ui::EventType type) { type_ = type; } | 45 void set_type(ui::EventType type) { type_ = type; } |
| 46 ui::EventType type() const { return type_; } |
90 | 47 |
91 int modifiers() const { return modifiers_; } | 48 int modifiers() const { return modifiers_; } |
92 | 49 |
93 bool IsShiftDown() const { | 50 bool IsShiftDown() const; |
94 return (modifiers_ & ui::EF_SHIFT_DOWN) == ui::EF_SHIFT_DOWN; | 51 bool IsCtrlDown() const; |
95 } | 52 bool IsAltDown() const; |
96 | |
97 bool IsCtrlDown() const { | |
98 return (modifiers_ & ui::EF_CONTROL_DOWN) == ui::EF_CONTROL_DOWN; | |
99 } | |
100 | |
101 bool IsAltDown() const { | |
102 return (modifiers_ & ui::EF_ALT_DOWN) == ui::EF_ALT_DOWN; | |
103 } | |
104 | 53 |
105 // Returns a string with the localized shortcut if any. | 54 // Returns a string with the localized shortcut if any. |
106 string16 GetShortcutText() const; | 55 string16 GetShortcutText() const; |
107 | 56 |
108 protected: | 57 protected: |
109 // The keycode (VK_...). | 58 // The keycode (VK_...). |
110 ui::KeyboardCode key_code_; | 59 KeyboardCode key_code_; |
111 | 60 |
112 // The event type (usually ui::ET_KEY_PRESSED). | 61 // The event type (usually ui::ET_KEY_PRESSED). |
113 ui::EventType type_; | 62 EventType type_; |
114 | 63 |
115 // The state of the Shift/Ctrl/Alt keys (platform-dependent). | 64 // The state of the Shift/Ctrl/Alt keys (platform-dependent). |
116 int modifiers_; | 65 int modifiers_; |
117 }; | 66 }; |
118 | 67 |
119 // An interface that classes that want to register for keyboard accelerators | 68 // An interface that classes that want to register for keyboard accelerators |
120 // should implement. | 69 // should implement. |
121 class UI_EXPORT AcceleratorTarget { | 70 class UI_EXPORT AcceleratorTarget { |
122 public: | 71 public: |
123 // Should return true if the accelerator was processed. | 72 // Should return true if the accelerator was processed. |
(...skipping 18 matching lines...) Expand all Loading... |
142 virtual bool GetAcceleratorForCommandId(int command_id, | 91 virtual bool GetAcceleratorForCommandId(int command_id, |
143 ui::Accelerator* accelerator) = 0; | 92 ui::Accelerator* accelerator) = 0; |
144 | 93 |
145 protected: | 94 protected: |
146 virtual ~AcceleratorProvider() {} | 95 virtual ~AcceleratorProvider() {} |
147 }; | 96 }; |
148 | 97 |
149 } // namespace ui | 98 } // namespace ui |
150 | 99 |
151 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ | 100 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ |
OLD | NEW |