Home | History | Annotate | Download | only in accelerators
      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_base_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_BASE_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 
     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.
     48   void set_type(ui::EventType type) { type_ = type; }
     49   ui::EventType type() const { return type_; }
     50 
     51   int modifiers() const { return modifiers_; }
     52 
     53   bool IsShiftDown() const;
     54   bool IsCtrlDown() const;
     55   bool IsAltDown() const;
     56   bool IsCmdDown() const;
     57   bool IsRepeat() const;
     58 
     59   // Returns a string with the localized shortcut if any.
     60   base::string16 GetShortcutText() const;
     61 
     62   void set_platform_accelerator(scoped_ptr<PlatformAccelerator> p) {
     63     platform_accelerator_ = p.Pass();
     64   }
     65 
     66   // This class keeps ownership of the returned object.
     67   const PlatformAccelerator* platform_accelerator() const {
     68     return platform_accelerator_.get();
     69   }
     70 
     71   void set_is_repeat(bool is_repeat) { is_repeat_ = is_repeat; }
     72 
     73  protected:
     74   // The keycode (VK_...).
     75   KeyboardCode key_code_;
     76 
     77   // The event type (usually ui::ET_KEY_PRESSED).
     78   EventType type_;
     79 
     80   // The state of the Shift/Ctrl/Alt keys.
     81   int modifiers_;
     82 
     83   // True if the accelerator is created for an auto repeated key event.
     84   bool is_repeat_;
     85 
     86   // Stores platform specific data. May be NULL.
     87   scoped_ptr<PlatformAccelerator> platform_accelerator_;
     88 };
     89 
     90 // An interface that classes that want to register for keyboard accelerators
     91 // should implement.
     92 class UI_BASE_EXPORT AcceleratorTarget {
     93  public:
     94   // Should return true if the accelerator was processed.
     95   virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0;
     96 
     97   // Should return true if the target can handle the accelerator events. The
     98   // AcceleratorPressed method is invoked only for targets for which
     99   // CanHandleAccelerators returns true.
    100   virtual bool CanHandleAccelerators() const = 0;
    101 
    102  protected:
    103   virtual ~AcceleratorTarget() {}
    104 };
    105 
    106 // Since accelerator code is one of the few things that can't be cross platform
    107 // in the chrome UI, separate out just the GetAcceleratorForCommandId() from
    108 // the menu delegates.
    109 class AcceleratorProvider {
    110  public:
    111   // Gets the accelerator for the specified command id. Returns true if the
    112   // command id has a valid accelerator, false otherwise.
    113   virtual bool GetAcceleratorForCommandId(int command_id,
    114                                           ui::Accelerator* accelerator) = 0;
    115 
    116  protected:
    117   virtual ~AcceleratorProvider() {}
    118 };
    119 
    120 }  // namespace ui
    121 
    122 #endif  // UI_BASE_ACCELERATORS_ACCELERATOR_H_
    123