• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
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 
11 #ifndef UI_BASE_ACCELERATORS_ACCELERATOR_H_
12 #define UI_BASE_ACCELERATORS_ACCELERATOR_H_
13 
14 #include "base/memory/scoped_ptr.h"
15 #include "base/strings/string16.h"
16 #include "ui/base/accelerators/platform_accelerator.h"
17 #include "ui/base/ui_export.h"
18 #include "ui/events/event_constants.h"
19 #include "ui/events/keycodes/keyboard_codes.h"
20 
21 namespace ui {
22 
23 class PlatformAccelerator;
24 
25 // This is a cross-platform class for accelerator keys used in menus.
26 // |platform_accelerator| should be used to store platform specific data.
27 class UI_EXPORT Accelerator {
28  public:
29   Accelerator();
30   Accelerator(ui::KeyboardCode keycode, int modifiers);
31   Accelerator(const Accelerator& accelerator);
32   ~Accelerator();
33 
34   Accelerator& operator=(const Accelerator& accelerator);
35 
36   // Define the < operator so that the KeyboardShortcut can be used as a key in
37   // a std::map.
38   bool operator <(const Accelerator& rhs) const;
39 
40   bool operator ==(const Accelerator& rhs) const;
41 
42   bool operator !=(const Accelerator& rhs) const;
43 
key_code()44   ui::KeyboardCode key_code() const { return key_code_; }
45 
46   // Sets the event type if the accelerator should be processed on an event
47   // other than ui::ET_KEY_PRESSED.
set_type(ui::EventType type)48   void set_type(ui::EventType type) { type_ = type; }
type()49   ui::EventType type() const { return type_; }
50 
modifiers()51   int modifiers() const { return modifiers_; }
52 
53   bool IsShiftDown() const;
54   bool IsCtrlDown() const;
55   bool IsAltDown() const;
56   bool IsCmdDown() const;
57 
58   // Returns a string with the localized shortcut if any.
59   base::string16 GetShortcutText() const;
60 
set_platform_accelerator(scoped_ptr<PlatformAccelerator> p)61   void set_platform_accelerator(scoped_ptr<PlatformAccelerator> p) {
62     platform_accelerator_ = p.Pass();
63   }
64 
65   // This class keeps ownership of the returned object.
platform_accelerator()66   const PlatformAccelerator* platform_accelerator() const {
67     return platform_accelerator_.get();
68   }
69 
70 
71  protected:
72   // The keycode (VK_...).
73   KeyboardCode key_code_;
74 
75   // The event type (usually ui::ET_KEY_PRESSED).
76   EventType type_;
77 
78   // The state of the Shift/Ctrl/Alt keys.
79   int modifiers_;
80 
81   // Stores platform specific data. May be NULL.
82   scoped_ptr<PlatformAccelerator> platform_accelerator_;
83 };
84 
85 // An interface that classes that want to register for keyboard accelerators
86 // should implement.
87 class UI_EXPORT AcceleratorTarget {
88  public:
89   // Should return true if the accelerator was processed.
90   virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0;
91 
92   // Should return true if the target can handle the accelerator events. The
93   // AcceleratorPressed method is invoked only for targets for which
94   // CanHandleAccelerators returns true.
95   virtual bool CanHandleAccelerators() const = 0;
96 
97  protected:
~AcceleratorTarget()98   virtual ~AcceleratorTarget() {}
99 };
100 
101 // Since accelerator code is one of the few things that can't be cross platform
102 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from
103 // the menu delegates.
104 class AcceleratorProvider {
105  public:
106   // Gets the accelerator for the specified command id. Returns true if the
107   // command id has a valid accelerator, false otherwise.
108   virtual bool GetAcceleratorForCommandId(int command_id,
109                                           ui::Accelerator* accelerator) = 0;
110 
111  protected:
~AcceleratorProvider()112   virtual ~AcceleratorProvider() {}
113 };
114 
115 }  // namespace ui
116 
117 #endif  // UI_BASE_ACCELERATORS_ACCELERATOR_H_
118