accelerator.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/string16.h" 15#include "ui/base/events/event_constants.h" 16#include "ui/base/keycodes/keyboard_codes.h" 17#include "ui/base/ui_export.h" 18 19namespace ui { 20 21// This is a cross-platform base class for accelerator keys used in menus. It is 22// meant to be subclassed for concrete toolkit implementations. 23class UI_EXPORT Accelerator { 24 public: 25 Accelerator(); 26 Accelerator(ui::KeyboardCode keycode, int modifiers); 27 Accelerator(const Accelerator& accelerator); 28 ~Accelerator(); 29 30 Accelerator& operator=(const Accelerator& accelerator); 31 32 // We define the < operator so that the KeyboardShortcut can be used as a key 33 // in a std::map. 34 bool operator <(const Accelerator& rhs) const; 35 36 bool operator ==(const Accelerator& rhs) const; 37 38 bool operator !=(const Accelerator& rhs) const; 39 40 ui::KeyboardCode key_code() const { return key_code_; } 41 42 // Sets the event type if the accelerator should be processed on an event 43 // other than ui::ET_KEY_PRESSED. 44 void set_type(ui::EventType type) { type_ = type; } 45 ui::EventType type() const { return type_; } 46 47 int modifiers() const { return modifiers_; } 48 49 bool IsShiftDown() const; 50 bool IsCtrlDown() const; 51 bool IsAltDown() const; 52 bool IsCmdDown() const; 53 54 // Returns a string with the localized shortcut if any. 55 string16 GetShortcutText() const; 56 57 protected: 58 // The keycode (VK_...). 59 KeyboardCode key_code_; 60 61 // The event type (usually ui::ET_KEY_PRESSED). 62 EventType type_; 63 64 // The state of the Shift/Ctrl/Alt keys (platform-dependent). 65 int modifiers_; 66}; 67 68// An interface that classes that want to register for keyboard accelerators 69// should implement. 70class UI_EXPORT AcceleratorTarget { 71 public: 72 // Should return true if the accelerator was processed. 73 virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0; 74 75 // Should return true if the target can handle the accelerator events. The 76 // AcceleratorPressed method is invoked only for targets for which 77 // CanHandleAccelerators returns true. 78 virtual bool CanHandleAccelerators() const = 0; 79 80 protected: 81 virtual ~AcceleratorTarget() {} 82}; 83 84// Since accelerator code is one of the few things that can't be cross platform 85// in the chrome UI, separate out just the GetAcceleratorForCommandId() from 86// the menu delegates. 87class AcceleratorProvider { 88 public: 89 // Gets the accelerator for the specified command id. Returns true if the 90 // command id has a valid accelerator, false otherwise. 91 virtual bool GetAcceleratorForCommandId(int command_id, 92 ui::Accelerator* accelerator) = 0; 93 94 protected: 95 virtual ~AcceleratorProvider() {} 96}; 97 98} // namespace ui 99 100#endif // UI_BASE_ACCELERATORS_ACCELERATOR_H_ 101