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