15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class describe a keyboard accelerator (or keyboard shortcut).
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Keyboard accelerators are registered with the FocusManager.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It has a copy constructor and assignment operator so that it can be copied.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// It also defines the < operator so that it can be used as a key in a std::map.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_BASE_ACCELERATORS_ACCELERATOR_H_
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_BASE_ACCELERATORS_ACCELERATOR_H_
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
155e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string16.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/accelerators/platform_accelerator.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/ui_base_export.h"
18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event_constants.h"
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/keycodes/keyboard_codes.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PlatformAccelerator;
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This is a cross-platform class for accelerator keys used in menus.
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// |platform_accelerator| should be used to store platform specific data.
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class UI_BASE_EXPORT Accelerator {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Accelerator();
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Accelerator(ui::KeyboardCode keycode, int modifiers);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Accelerator(const Accelerator& accelerator);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~Accelerator();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Accelerator& operator=(const Accelerator& accelerator);
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Define the < operator so that the KeyboardShortcut can be used as a key in
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // a std::map.
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator <(const Accelerator& rhs) const;
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator ==(const Accelerator& rhs) const;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator !=(const Accelerator& rhs) const;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui::KeyboardCode key_code() const { return key_code_; }
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the event type if the accelerator should be processed on an event
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other than ui::ET_KEY_PRESSED.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void set_type(ui::EventType type) { type_ = type; }
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui::EventType type() const { return type_; }
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int modifiers() const { return modifiers_; }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsShiftDown() const;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsCtrlDown() const;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsAltDown() const;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsCmdDown() const;
5746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool IsRepeat() const;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a string with the localized shortcut if any.
60a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  base::string16 GetShortcutText() const;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void set_platform_accelerator(scoped_ptr<PlatformAccelerator> p) {
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    platform_accelerator_ = p.Pass();
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This class keeps ownership of the returned object.
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const PlatformAccelerator* platform_accelerator() const {
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return platform_accelerator_.get();
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_is_repeat(bool is_repeat) { is_repeat_ = is_repeat; }
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The keycode (VK_...).
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  KeyboardCode key_code_;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The event type (usually ui::ET_KEY_PRESSED).
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EventType type_;
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The state of the Shift/Ctrl/Alt keys.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int modifiers_;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // True if the accelerator is created for an auto repeated key event.
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool is_repeat_;
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Stores platform specific data. May be NULL.
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<PlatformAccelerator> platform_accelerator_;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// An interface that classes that want to register for keyboard accelerators
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// should implement.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class UI_BASE_EXPORT AcceleratorTarget {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should return true if the accelerator was processed.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool AcceleratorPressed(const Accelerator& accelerator) = 0;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should return true if the target can handle the accelerator events. The
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AcceleratorPressed method is invoked only for targets for which
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // CanHandleAccelerators returns true.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanHandleAccelerators() const = 0;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~AcceleratorTarget() {}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Since accelerator code is one of the few things that can't be cross platform
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the chrome UI, separate out just the GetAcceleratorForCommandId() from
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the menu delegates.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AcceleratorProvider {
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the accelerator for the specified command id. Returns true if the
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // command id has a valid accelerator, false otherwise.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool GetAcceleratorForCommandId(int command_id,
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          ui::Accelerator* accelerator) = 0;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~AcceleratorProvider() {}
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // UI_BASE_ACCELERATORS_ACCELERATOR_H_
123