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_; } | |
mazda
2012/02/13 08:36:06
nit: insert an empty line
Yusuke Sato
2012/02/13 08:45:32
Done.
| |
86 // Sets the event type if the accelerator should be processed on an event | |
87 // other than ui::ET_KEY_PRESSED. | |
88 void set_type(ui::EventType type) { type_ = type; } | |
89 | |
77 int modifiers() const { return modifiers_; } | 90 int modifiers() const { return modifiers_; } |
78 | 91 |
79 bool IsShiftDown() const { | 92 bool IsShiftDown() const { |
80 return (modifiers_ & ui::EF_SHIFT_DOWN) == ui::EF_SHIFT_DOWN; | 93 return (modifiers_ & ui::EF_SHIFT_DOWN) == ui::EF_SHIFT_DOWN; |
81 } | 94 } |
82 | 95 |
83 bool IsCtrlDown() const { | 96 bool IsCtrlDown() const { |
84 return (modifiers_ & ui::EF_CONTROL_DOWN) == ui::EF_CONTROL_DOWN; | 97 return (modifiers_ & ui::EF_CONTROL_DOWN) == ui::EF_CONTROL_DOWN; |
85 } | 98 } |
86 | 99 |
87 bool IsAltDown() const { | 100 bool IsAltDown() const { |
88 return (modifiers_ & ui::EF_ALT_DOWN) == ui::EF_ALT_DOWN; | 101 return (modifiers_ & ui::EF_ALT_DOWN) == ui::EF_ALT_DOWN; |
89 } | 102 } |
90 | 103 |
91 // Returns a string with the localized shortcut if any. | 104 // Returns a string with the localized shortcut if any. |
92 string16 GetShortcutText() const; | 105 string16 GetShortcutText() const; |
93 | 106 |
94 protected: | 107 protected: |
95 // The keycode (VK_...). | 108 // The keycode (VK_...). |
96 ui::KeyboardCode key_code_; | 109 ui::KeyboardCode key_code_; |
97 | 110 |
111 // The event type (usually ui::ET_KEY_PRESSED). | |
112 ui::EventType type_; | |
113 | |
98 // The state of the Shift/Ctrl/Alt keys (platform-dependent). | 114 // The state of the Shift/Ctrl/Alt keys (platform-dependent). |
99 int modifiers_; | 115 int modifiers_; |
100 }; | 116 }; |
101 | 117 |
102 // An interface that classes that want to register for keyboard accelerators | 118 // An interface that classes that want to register for keyboard accelerators |
103 // should implement. | 119 // should implement. |
104 class UI_EXPORT AcceleratorTarget { | 120 class UI_EXPORT AcceleratorTarget { |
105 public: | 121 public: |
106 // Should return true if the accelerator was processed. | 122 // Should return true if the accelerator was processed. |
107 virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0; | 123 virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0; |
(...skipping 17 matching lines...) Expand all Loading... | |
125 virtual bool GetAcceleratorForCommandId(int command_id, | 141 virtual bool GetAcceleratorForCommandId(int command_id, |
126 ui::Accelerator* accelerator) = 0; | 142 ui::Accelerator* accelerator) = 0; |
127 | 143 |
128 protected: | 144 protected: |
129 virtual ~AcceleratorProvider() {} | 145 virtual ~AcceleratorProvider() {} |
130 }; | 146 }; |
131 | 147 |
132 } // namespace ui | 148 } // namespace ui |
133 | 149 |
134 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ | 150 #endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ |
OLD | NEW |